11

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

Meltemi
  • 37,979
  • 50
  • 195
  • 293

6 Answers6

25

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.

Jaime Bellmyer
  • 23,051
  • 7
  • 53
  • 50
  • holy crap! you da man! i've been staring at those error messages for hours...and trying everything i could think of... cursing Rails all along the way! THANK YOU! – Meltemi Dec 03 '10 at 03:45
  • just out of curiosity what/how/where should I be looking for clues if something like this comes up again?!? – Meltemi Dec 03 '10 at 03:50
  • Well, this is a one-off issue. If javascript related stuff isn't working, the layout is probably missing the include command. But if you're looking for other Rails 3 transition stuff, check out railscasts.com - Ryan Bates has created some awesome screencasts on migrating to Rails 3. – Jaime Bellmyer Dec 03 '10 at 05:03
  • 1
    for some reason, this solution is not working for me with Rails4. I always see, 'No route matches [GET] "/users/sign_out"' – Som Poddar Jul 11 '14 at 18:20
18

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" %>
AllenC
  • 2,754
  • 1
  • 41
  • 74
  • 1
    Best answer because show a way to load just the required file, instead others that show how to include all files, what is bad for performance. – Fernando Kosh Aug 07 '13 at 03:51
  • This is the answer that helped me. I needed to make sure to have this in application.js: `//= require jquery_ujs` – Seth Jeffery Sep 11 '15 at 08:03
12

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" %>
Victor Augusto
  • 2,406
  • 24
  • 20
2

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.

Abhi
  • 3,361
  • 2
  • 33
  • 38
  • Thanks nice workarround.. I was facing problem with some popover plugin in which the link was submitting as get request. This worked for me. – jbmyid Jul 07 '14 at 06:35
1

check if application.html.erb

<%= javascript_include_tag :application %>

not as:

<%= javascript_include_tag :default %>
Mohamed Yakout
  • 2,868
  • 1
  • 25
  • 45
0

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 %>
Sam Ritchie
  • 10,988
  • 4
  • 42
  • 53