41

I'm using Rails 3 and have have a page that outputs a list of posts from the database. I'd like to be able to delete a post from a link.

The 2nd example below works, but the first doesn't. Anybody know why the first won't work? My view contains:

# this link sends a "GET" request which asks for the #show function  
<%= link_to 'Delete', post, :method => :delete %>

# this link sends the proper "DELETE" request which asks for the #destroy function
<%= button_to 'Delete', post, :method => :delete %>

My routes file contains the following:

resources :posts 
vonconrad
  • 25,227
  • 7
  • 68
  • 69
Jamis Charles
  • 5,827
  • 8
  • 32
  • 42

9 Answers9

48

Make sure that your <head> section in the layout includes the following:

<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

In Rails 3, the delete requests are being handled with the help of JavaScript to ensure that the request is being sent correctly. If you don't have the csrf meta tags and the required JavaScript code, delete links won't work. The :defaults JavaScript files include--among others--prototype.js, and application.js. The latter contains the stuff that makes the link work, but relies on the Prototype framework.

If you don't want to use the default Prototype JavaScript libraries, there are ports of application.js for several other frameworks. You can find the jQuery one here, for instance.

Buttons will still work regardless of JavaScript, as the HTML code generated with button_to includes all necessary information by itself. This is why you're seeing the button work while the link doesn't.

vonconrad
  • 25,227
  • 7
  • 68
  • 69
17

looks like

<%= javascript_include_tag :defaults %>

is no longer supported

http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/JavascriptTagHelpers/javascript_include_tag

Instead, you should use

<%= javascript_include_tag 'application' %>
alikk
  • 503
  • 6
  • 11
  • This is true for Rails 4.0.3. I tried applying the accepted answer, and it didn't work until I made the substitution you point out here. – Morgan Laco Sep 07 '14 at 18:04
8

I had the same problem in rails 5.2.3 and this helped me:

layout head:

<%= javascript_include_tag "rails-ujs" %>

config/initializers/assets

Rails.application.config.assets.precompile += %w( rails-ujs.js )
Romalex
  • 1,582
  • 11
  • 13
3

In my case, I was using following.

<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

I was also using jQuery by including jQuery just below default prototype JS. That was causing some sort of conflict.

Ninad
  • 957
  • 7
  • 19
2

Destructive actions should be performed as a form submission - http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist

You can style the button appropriately for delete method or Use Form.

Omer Aslam
  • 4,534
  • 6
  • 25
  • 24
1

I had the same issue in Rails 6. The solution by Romalex to include:

<%= javascript_include_tag "rails-ujs" %> in head tag & Rails.application.config.assets.precompile += %w( rails-ujs.js ) in config/initializers/assets also worked to solve an issue with destroy_user_session in devise which would not DELETE.

So this solution also applies to this question Ruby on Rails - devise users/sign_out not working

T_M
  • 11
  • 2
0

This will work. The issue is the fact that you need to pass it the right path so the route gets it.

<%= link_to 'Delete', post_path(post.id), :method => :delete %>
FlyingV
  • 2,207
  • 19
  • 18
0

In my case with Rails 5, I was using following in my layout:

<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

In this way:

... </title>
  <%= csrf_meta_tags %>
  <%= csp_meta_tag %>
...
 <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
 </head>
matiasmasca
  • 605
  • 8
  • 14
-1

Try this.. its a simple change..

add this to your route.rb file

get '/lists/:list_id/comments/:id' => 'comments#destroy'

I made this change and now its working..

lordzeus1989
  • 55
  • 1
  • 7