Delete link sends "Get" instead of "Delete" in Rails 3 view

Viewed 16525

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 
9 Answers

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 )

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

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>
Related