jay
jay

Reputation: 12495

Link_to :method => :delete not routing to destroy in Rails 3 with jQuery

I'm trying to make this be a link to a destroy action for a note :

 <%= link_to "delete", @note, :method => :delete, :confirm => "Are you sure" %>

This just routes to the note and doesn't go through to the destroy action... Whereas this:

 <%= button_to "delete", @note, :method => :delete, :confirm => "Are you sure" %>

Does route properly. I'm using jQuery 1.7 and I installed the gem 'jquery-rails', '>= 1.0.12' (which includes the jquery_ujs.js file).

What fixes are there to make link_to work properly? For some reason, I have an older app on which these link_to links work, but I can't remember why.. Any pointers?

(I really don't want to use button_to.. this styling is annoying, and I'm sure that there must be a way to do this..)

UPDATE: Using Rails 3.0.9

Upvotes: 4

Views: 3432

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107728

You're not loading the jquery_ujs.js file from jquery-rails. Ensure that you're requiring both jquery and jquery_ujs in your layout.

These two files are typically included in the app/assets/javascripts/application.js file, which contains this content:

//= require jquery
//= require jquery_ujs
//= require_tree .

If you're not including this in your layout with this line:

<%= javascript_include_tag "application" %>

Then both the jquery.js and jquery_ujs.js files won't be included, and so the :method => :delete requests will not work as intended.

Upvotes: 11

Related Questions