44

I am working through the Getting Started tutorial (creating a Blog) and the link_to Destroy is not functioning properly. In the terminal it always interprets it as #SHOW.

In reading similar issues I have learned that the Delete must be converted to a POST for the browser to interpret it. This doesn't seem to be happening.

I have run into the same problem using Destroy in the Lynda.com Rails course as well, so it leads me to believe it is something in my development environment. I am using Rails 4, Ruby 2.00p274, MySQL, WEBrick for the HTTP server on a MacBook Pro Lion.

in the terminal session when Destroy is selected:

Started GET "/posts/4" for 127.0.0.1 at 2013-08-09 13:45:20 -0600
Processing by PostsController#show as HTML
  Parameters: {"id"=>"4"}
  Post Load (0.6ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", "4"]]
  Rendered posts/show.html.erb within layouts/application (0.4ms)
Completed 200 OK in 13ms (Views: 8.6ms | ActiveRecord: 0.6ms)

In the ports-controller.rb:

def destroy
    @post = Post.find(params[:id])
    @post.destroy

    redirect_to action: :index
end

In the index.html.erb:

<% @posts.each do |post| %>
  <tr>
    <td><%= post.title %></td>
     <td><%= post.text %></td>
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy',  { action: :destroy, id: post.id }, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
 <% end %>

In the routes.rb

Blog::Application.routes.draw do
   resources :posts do
     resources :comments
   end
  root to: 'welcome#index'
end
hyperview.ca
  • 541
  • 1
  • 4
  • 3
  • Show your layouts/application.html.erb and app/assets/application.js. – Marek Lipka Aug 09 '13 at 20:21
  • 4
    You have to use the `jquery_ujs` library included by default in a new Rails application to make `:delete` methods POST to the endpoint as expected. – Ross Allen Aug 09 '13 at 20:39
  • @ssorallen: Post that as an answer! I had exactly the same problem, and the answers given yet do not help, but including jquery_ujs into the file `application.js` did solve it for me. Perhaps only a problem when you migrate ... – mliebelt Aug 10 '13 at 09:38
  • Possible duplicate of [Delete link sends "Get" instead of "Delete" in Rails 3 view](http://stackoverflow.com/questions/3774925/delete-link-sends-get-instead-of-delete-in-rails-3-view) – Bryan Ash Mar 09 '16 at 13:43

13 Answers13

56

Try this

<%= link_to 'Destroy',  post,  method: :delete, data: { confirm: 'Are you sure?' } %>
djot
  • 2,952
  • 4
  • 19
  • 28
hawk
  • 5,409
  • 2
  • 23
  • 29
42

Make sure gem 'jquery-rails' is in gemfile and jquery_ujs is included in app/assets/javascripts/application.js file

//= require jquery
//= require jquery_ujs

Source: https://github.com/rails/jquery-ujs

rlshep
  • 580
  • 4
  • 13
  • 3
    In core rails app its by default, but not when you creating an engine – Outside_Box Nov 24 '16 at 17:44
  • +1 In my case `application.js` was missing `jquery_ujs`. I guess I have removed it while following the installation process from a twitter bootstrap gem –  Jan 07 '17 at 14:01
  • FYI, the plain jquery line is required as well (so you need both not just the jquery_ujs one) – Sia Jan 29 '19 at 20:15
18

I had the same issue, for rails 4.2.X. Checked all my javascript files but could not make it work. if u look closely at the server request u will be missing 'authenticity_token' in the params, so user gets logged out. In rails 4.1 and above u have to use button_to instead of link_to

nowRails
  • 267
  • 2
  • 5
10

I solved the problem, just by adding in assets/javascripts/application.js

//= require jquery_ujs
Marcelo Bento
  • 101
  • 1
  • 3
4

Make sure that you have a Delete REST method via rake routes.

DELETE /articles/:id(.:format)                     articles#destroy

I've got the same problem only with this blog version as I'm doing both. http://blog.8thcolor.com/en/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/

I'm also trying to learn how to use the web console and pry within it. My problem is that binding.pry doesn't show up in destroy method but will in show. That tells me it must be a bad link right? It's not getting to the destroy method even. Thank you all for your answers. We do have to try things don't we? Trying

<td><%= link_to 'Destroy', { action: :destroy, id: post.id }, method: :delete, data: { confirm: 'Are you sure?' } %></td> 

is not going to work for that one.

Here's what he has

<td><%= link_to 'Destroy', [comment.post, comment], :confirm => 'Are you sure?', :method => :delete %></td>

because you only want to delete the comment here.

But combining things so far like the following gives no errors but I still have no binding.pry from the destroy method in the controller.

<td><%= link_to 'Destroy', [comment.post, comment], method: :delete, data: { confirm: 'Are you sure?' } %></td>

and I never get the confirmation like you would expect.

So do try to figure out what's going on with the jquery as suspect. I can confirm that was my case but the rest I'll leave for nubes because this will show up in a google search.

Douglas G. Allen
  • 2,203
  • 21
  • 20
2

Started GET "/posts/4" for 127.0.0.1 at 2013-08-09 13:45:20 -0600

This is the problem. The delete link is using GET http verb even though you used method: delete in your link.

Check out the following SO thread

Community
  • 1
  • 1
Jason Kim
  • 18,102
  • 13
  • 66
  • 105
2

include below line in js

//= require jquery_ujs

include gem:

gem 'jquery-rails'

Destroy Link:

<%= link_to "Logout", destroy_user_session_path %>
Chitresh goyal
  • 313
  • 1
  • 7
1

I had the same issue and realized that I had created my rails app with -J which is the same as --skip-javascript. You need JavaScript enabled for this to work; the lack of a popup to confirm the delete is a dead giveaway.

1jgjgjg
  • 11
  • 1
0

I had exactly this problem and it was caused by triple-clicking the Guide's code block and copy/pasting the code straight into my editor (ST2 on OSX).

You should check that the generated HTML for the link looks like this:

<a data-confirm="Are you sure?" data-method="delete" href="/posts/3/comments/3" rel="nofollow">Destroy Comment</a>

If it doesn't, but instead looks like the below, then the Javascript won't be applied:

<a href="/posts/3/comments/3"                data="{:confirm=&gt;&quot;Are you sure?&quot;}"                method="delete">Destroy Comment</a>

The large gaps between the href and the unparsed Ruby are caused by the non-breaking spaces (unicode character \u00a0) used in the Guide being copied into your script.

I'm not sure why this doesn't cause a script parse error.

Barry
  • 746
  • 9
  • 20
0

I had the same problem as Barry. Make sure you're copy/pasting correctly in your /app/views/index.html.erb file and inspect the html that is rendered.

danjonesgh
  • 93
  • 2
  • 9
0

I had same problem. In my case, a i had change \app\views\layouts\application.html.erb file from

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

to

<%= stylesheet_link_tag    'default', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>

to avoid my very first Rails problem. But it apparently mangled JavaScript execution and caused Destroy issue. Therefore rollback your \app\views\layouts\application.html.erb file to its original state, and treat this problem as here

Rails ExecJS::ProgramError in Pages#home?

in evedovelli answer (about coffeescript gem in windows).

Community
  • 1
  • 1
Enlighten
  • 1
  • 2
0

I got the same problem. I work out by this way:

My env. is: A. ruby 2.2.4p230 (2015-12-16 revision 53155) [x64-mingw32] B. Rails 4.2.5 C. Browser: Firfox 44.02

  1. add include js in html page because delete function will use js to handle. <%= javascript_include_tag "application" %>

  2. add skip_before_action :verify_authenticity_token to controller for prevent InvalidAuthenticityToken in AdsController#destroy

  3. add link_to into your web page <%= link_to 'Delete', post, method: :delete, data: {confirm: "Are you sure?"}%>

    Now, I can use link_to to delete a record from my page.

Sam Sheen
  • 21
  • 3
0

Using the button_to method instead of link_to seemed to work, minus the fact that it was not confirming(which I felt was important).

In my case, I was using react-on-rails and had multiple layouts.

Adding <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> to my hello-world layout fixed the problem.