Maxence
Maxence

Reputation: 2339

Cannot Delete Entry following Getting Started RAILS Tutorial

I have a problem that has been documented previously but for which I can't solve in my case. This is about the 5.13 section of Getting Started Rails tutorial that deals with deleting an entry.

First here is my Controller :

 def destroy
 @article = Article.find(params[:id])
 @article.destroy

  redirect_to articles_path   end

My view

<% @articles.each do |article| %>
  <tr>
    <td><%= article.title %></td>
    <td><%= article.text %></td>
    <td><%= link_to 'Show', article_path(article) %></td>
     <td><%= link_to 'Edit', edit_article_path(article) %></td>
    <td><%= link_to 'Destroy', article_path(article), method: :delete, data: { confirm: 'Are you sure?' } %></td>
   </tr>   <% end %>

As per this thread : Rails 4 link_to Destroy not working in Getting Started tutorial

I have amended link_to to button_to which DOES work. But this doesn't really explain why the tutorial bit doesn't work.

I have also made sure that the 'jquery-rails' gem was in the gemfile. And I checked aswell the follwing from my app/assets/javascript/application.js file:

//= require jquery 
//= require jquery_ujs 
//= require turbolinks
//= require_tree .

As per this thread Delete link sends "Get" instead of "Delete" in Rails 3 view I checked my app/views/layouts/application.html.erb file which looks like :

<!DOCTYPE html> 
<html> 
<head>   
<title></title>   
<%= stylesheet_link_tag    'default', media: 'all', 'data-turbolinks-track' => true %>   
<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %> 
<%= csrf_meta_tags %> 
</head> <body>

<%= yield %>

</body> 
</html>

I tried in both Chrome and Firefox. Still doesnt work.

Upvotes: 0

Views: 170

Answers (2)

Ansd
Ansd

Reputation: 1865

If you are copying and pasting directly from Rails website, there is a chance you got your indentation mixed. Try cleaning up your code and stick to one indentation style e.g. taps or spaces.

Upvotes: 0

AdamCooper86
AdamCooper86

Reputation: 4237

If the question is why does it have to be button_to instead of link_to, it is pretty straight forward. Modern browswers do not implement verbs other than GET for links(anchor tags). It has to do with the history of the web and browsers. Rails requires a DELETE verb to look for the destroy action. To get around the constraint, button_to generates a form with the correct verb (method in rails speak), and the correct path.

Upvotes: 1

Related Questions