{"action":"closed","issue":{"url":"https://api.github.com/repos/ga-dc/pbj-project2/issues/178","labels_url":"https://api.github.com/repos/ga-dc/pbj-project2/issues/178/labels{/name}","comments_url":"https://api.github.com/repos/ga-dc/pbj-project2/issues/178/comments","events_url":"https://api.github.com/repos/ga-dc/pbj-project2/issues/178/events","html_url":"https://github.com/ga-dc/pbj-project2/issues/178","id":99508540,"number":178,"title":"Ajax post requests in my base, killin my d00dz (where d00dz = authenticity tokens)","user":{"login":"segheysens","id":3592134,"avatar_url":"https://avatars.githubusercontent.com/u/3592134?v=3","gravatar_id":"","url":"https://api.github.com/users/segheysens","html_url":"https://github.com/segheysens","followers_url":"https://api.github.com/users/segheysens/followers","following_url":"https://api.github.com/users/segheysens/following{/other_user}","gists_url":"https://api.github.com/users/segheysens/gists{/gist_id}","starred_url":"https://api.github.com/users/segheysens/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/segheysens/subscriptions","organizations_url":"https://api.github.com/users/segheysens/orgs","repos_url":"https://api.github.com/users/segheysens/repos","events_url":"https://api.github.com/users/segheysens/events{/privacy}","received_events_url":"https://api.github.com/users/segheysens/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ga-dc/pbj-project2/labels/Waiting%20on%20student%20response","name":"Waiting on student response","color":"d4c5f9"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-08-06T19:35:27Z","updated_at":"2015-08-06T20:36:58Z","closed_at":"2015-08-06T20:36:58Z","body":"So I think I'm 90% of the way to getting a Ajax request to properly render the results of a PgSearch, but when I make the Post request from the Ajax call, it is messing with my authenticity tokens:\r\n\"ActionController::InvalidAuthenticityToken in StaticPagesController#results\" (StaticPagesController#results is the controller#action for my post requests to the /search page)\r\n\r\nI already tried making the recommended fix from the top comment on this post: http://stackoverflow.com/questions/20875591/actioncontrollerinvalidauthenticitytoken-in-registrationscontrollercreate\r\n \r\n*Related code* (looks like a lot, but thoroughly commented so it should be easy to follow)\r\n/app/view/static_pages/search.html.erb\r\n```html\r\n<script src=\"http://code.jquery.com/jquery-1.11.0.min.js\"></script>\r\n<script src=\"./js/ajaxRequestjQuery\"></script>\r\n\r\n<form action=\"/search\" method=\"post\">\r\n<input type=\"text\" size=\"30\" id=\"search_term\">\r\n</form>\r\n\r\n<ul id=\"search_results\">\r\n\r\n</ul>\r\n```\r\n\r\n/app/views/static_pages/js/ajaxRequestjQuery.js\r\n```javascript\r\n$(document).ready( function() {\r\n\r\n  // Event listener for when the search term gets submitted\r\n  $(\"#search_term\").submit(function(searchEvent){\r\n    // Keep the submit from refreshing the page\r\n    searchEvent.preventDefault();\r\n    // Obtain the value in the search field\r\n    var term = $(\"#search_term\").val();\r\n\r\n    $.ajax({\r\n      // Retrieves \"/search\" from the form on search.html\r\n      url: $(this).attr(\"action\"),\r\n      // Make sure this is being routed as a Post request\r\n      type: \"POST\",\r\n      // Only data to pass is the search field's term\r\n      data: {searchTerm: term},\r\n      // Callback function for a successful post:\r\n      // Update the search_results ul with the response from the controller\r\n      // We are expecting this to be already formatted as valid html, just in a string\r\n      success: function(response){\r\n        // Add results to # $(\"#search_results\")\r\n        $('#search_results').html(response);\r\n      }\r\n    });\r\n  });\r\n}); // end .ready\r\n```\r\n\r\n/app/controllers/static_pages_controller - definition for results Post request\r\n```ruby\r\ndef results\r\n    # search_term captures the data parameter sent form the Ajax call in ajaxRequestjQuery.js\r\n    @search_term = params[:searchTerm]\r\n    # results returns the PgSearch::Document with search results tied to the search_term\r\n    @results = PgSearch.multisearch(@search_term)\r\n\r\n    # return_html is initializing the return value to the Post request/Ajax call\r\n    @return_html = \"\"\r\n\r\n    # This loop creates an <li> for each Product returned from the PgSearch\r\n    # It adds the product's name, id, price, and anchors/links it to the item's show page\r\n    @results.each do |result|\r\n      # Create a temp variable with the current product's data\r\n      @product = Product.find(result.id)\r\n      @return_html += \"<li><a href='#{user_product_path(@product.user.id, @product.id)}'>\"\r\n      @return_html += @product.name + \" (id #\"\r\n      @return_html += @product.id + \") - \"\r\n      @return_html += @product.price\r\n      @return_html += \"</a></li>\"\r\n    end\r\n\r\n    # This render actually returns the full list of products that were captured\r\n    #by the PgSearch, in html form (as the Ajax call was expecting)\r\n    render :html => @return_html\r\n  end\r\n```\r\n\r\n*Disclaimer: my \"search\" that falls under \"static_pages\" is no longer so static..."}}