dpieri
dpieri

Reputation: 166

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?

Upvotes: 4

Views: 14294

Answers (4)

Ryan
Ryan

Reputation: 11696

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

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.

Upvotes: 2

Jai Kumar Rajput
Jai Kumar Rajput

Reputation: 4217

type something like inside your rails application root:

rails g jquery:install

And then, inside your application.html.erb add the line

<%= javascript_include_tag :defaults %>

or explicitly (do not forget to include your jquery separately):

<%= javascript_include_tag :rails, :application %>

[EDIT: for Rails 3.1 or greater using the asset pipeline]

Use the gem jquery-rails (as mentioned above) and add the following lines to the app/assets/javascripts/application.js (if they are not there already) :

//= require jquery
//= require jquery_ujs

Hope this helps!

Upvotes: 0

JTE
JTE

Reputation: 1321

See this post for a solution:

Rails form_for :remote=>true is not calling js method

When changing the rails environment to JQuery, you may accidentally lose your jquery-ujs file.

Upvotes: 1

Nicolas Blanco
Nicolas Blanco

Reputation: 11299

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.

Upvotes: 6

Related Questions