Meltemi
Meltemi

Reputation: 38359

Rails 3 link_to (:method => :delete) not working

I'm having trouble with my verbs in Rails...

viewing a page for a resource (Dog) which has_many (Fleas). Embedded in dog's show.html.haml is a call to render @dog.fleas which automatically(?) finds & uses the template in "fleas/_flea.html.haml" to list each flea associated with said dog.

this displays correctly. whew! Now, next to each flea I've put a "Kill Flea" link that goes to a url: //localhost:3000/dogs/1/fleas/7. Which is generated by:

= link_to("Kill Flea", [ flea.dog, flea ], :method => :delete, :confirm => "Sure? A bunny will die")

but every time that link is clicked there is no confirmation... and it renders the flea's show.html page. it's as if it's using GET on /dogs/1/fleas/7 instead of DELETE?!?

ps- not worried about spiders & robots deleting things in my database... i'm just trying to learn Rails..and understand what's happening

Upvotes: 11

Views: 20821

Answers (6)

Victor Augusto
Victor Augusto

Reputation: 2426

Check your <%= javascript_include_tag %> in application.html.erb

Perhaps you are missing the "application" js

It must look at least like

<%= javascript_include_tag "application" %>

Upvotes: 12

abhilashak
abhilashak

Reputation: 3621

Here I am not using the javascript_include_tag :all, :application etc. I am using my own js files, with another custom name: javascript_include_tag :my_custom_file and I also had this issue. And I am not using '= require jquery' in the file, but javascript/jquery is working. I think Rails includes jquery by default. What I done is I changed the

link_to "Logout", destroy_user_session_path, :method => :delete 

to

 form_tag destroy_user_session_path, :method => :delete, :id => "logout_form" 
   link_to 'Logout', "#delete", :onclick =>  "$('#logout_form').submit();"

Now its working fine.

Upvotes: 2

Mohamed Yakout
Mohamed Yakout

Reputation: 3046

check if application.html.erb

<%= javascript_include_tag :application %>

not as:

<%= javascript_include_tag :default %>

Upvotes: 1

AllenC
AllenC

Reputation: 2754

Hi you can also try this:

application.html.erb:

<%= javascript_include_tag 'jquery_ujs' %>

OR

<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "jquery.rails.js" %>

Upvotes: 17

Jaime Bellmyer
Jaime Bellmyer

Reputation: 23317

Rails 3 uses unobtrusive javascript now. In Rails 2.3, erb would just shove all that messy javascript right into the link itself, in an onClick event. Now the javascript has been moved out of the link, and into external js files. Make sure you have this in your layout:

<%= javascript_include_tag :all %>

If you do have this, there might be deeper problems keeping your javascript from running, but this is the place to start. Let me know how it turns out.

Upvotes: 25

Sam Ritchie
Sam Ritchie

Reputation: 11038

Looks like your link_to method isn't quite right. Try using this code instead:

  <%= link_to 'Kill Flea', [flea.dog, flea], :confirm => 'Sure?', :method => :delete %>

Upvotes: 0

Related Questions