Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: link_to with :remote => true not working

I'm trying to implement a voting system kind of like the one here on Stack Overflow. (Using Rails 3) I want the voting to be done without a page reload, so I have this code

link_to("/tags/#{tag.id}/upVote", :remote => true )

And so in my /views/tags directory I have a file called _upVote.js.erb that I thought would be called when this link is clicked, but it is not. It is trying to process upVote as HTML and this is the error I get

Missing template tags/upVote with {:formats=>[:html]

Also, here is what I have in my routes file

match "tags/:id/upVote" => "tags#upVote"

Any ideas how I can get this to work?

like image 969
dpieri Avatar asked Jan 21 '23 04:01

dpieri


2 Answers

If you got this error message in a blank new page, that means that your remote call does not work and the call wasn't made via an Ajax request. You need to check that your layout correctly loads jQuery and the jQuery Rails connector available here : http://github.com/rails/jquery-ujs

Then use Firefox+Firebug to check that the call is really an Ajax call.

like image 132
Nicolas Blanco Avatar answered Jan 31 '23 17:01

Nicolas Blanco


I had this same problem. To resolve the problem, I followed

  • https://launchschool.com/blog/the-detailed-guide-on-how-ajax-works-with-ruby-on-rails
  • Rails 4 rendering a partial with ajax, jquery, :remote => true, and respond_to

And finally, I had to

Require both jquery and jquery_ujs were in the application.js manifest.
//= require jquery
//= require jquery_ujs

After all that, Rails ajax started working for me.

like image 44
Ryan Avatar answered Jan 31 '23 17:01

Ryan