Rails link_to with remote: true processing html instead of js after page refresh
ruby-on-rails, ruby-on-rails-3
Solution
Does your `app/assets/javascripts/application.js` contain
//= require jquery
//= require jquery_ujs
?
And your erb contains `<%= javascript_include_tag "application" %>`?
I was just struggling with a problem like this for HOURS and the last of those two points fixed it; I saw the first point mentioned in some other questions so I'll repeat it here.
Hope that helps.
(Credit where credit's due)
Problem
I have a search page in my app, where there is an ajax search form. The search form works properly, passing parameters to the model to filter the search, and the model returning a collection of results. Upon search submit, @results are rendered on the page. Each @result then has a link to an action on it, like: ``` <%=link_to "Message", message_user_path(:id => user.id), :remote => true%> ``` Where this action in the controller is: ``` respond_to :js, :html def message @user_id = params[:id] @user = User.find_by_id(@user_id) respond_to do |format| format.html format.js end end ``` and this responds with message.js.erb, which triggers a messaging panel to pop up with a message to the user. All of this is working correctly, checking the log I see the correct get request sent, and the correct format being processed: ``` Started GET "/users/3/message" Processing by UsersController#message as JS ``` However, if I refresh the page and try to click the same link that was working before, I get the error Template is Missing. Checking the log, I see that now there are two requests sent, first an html then the same js request. ``` Started GET "/users/4/message" Processing by StudentsController#message as HTML ... Completed 406 Not Acceptable in 3ms (ActiveRecord: 1.0ms) Started GET "/users/4/message" Processing by StudentsController#message as JS ``` The html request throws the missing template error. Does anyone know why refreshing the page causes rails to attempt to respond with an html request to a remote link? EDIT: routes.rb ``` resources :students do member do get 'message' end end ```