0

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.

Community
  • 1
  • 1
Maxence
  • 2,029
  • 4
  • 18
  • 37

2 Answers2

1

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.

AdamCooper86
  • 3,957
  • 1
  • 13
  • 19
  • As an aside, modern browser's don't have a delete verb for forms either, which is why the form that button_to creates has a method hidden field that is then interpreted by the rails router to get the delete verb. – AdamCooper86 Dec 05 '15 at 05:34
  • Thanks for your answer; But it's no Javascript problem then ? I thought something was wrong with JS initially as I can't even see the popup window with the warning.. – Maxence Dec 05 '15 at 12:27
  • From what I remember of the Getting started in Rails, there aren't any javascript libraries that are being used to make links with Delete action work. Rails natively doesn't include any. I'd imagine the confirm isn't firing because the link isn't valid according to the browser. – AdamCooper86 Dec 05 '15 at 15:30
  • Would there be a way to be able to send a DELETE request to the server without needing a button? This is mainly for CSS considerations: I am not sure I can tweak a button in looking like a plain link.. (well I may bump into this later in my Rail training ?? ^^ but just in case you know ..) – Maxence Dec 15 '15 at 00:30
0

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.

Ansd
  • 1,865
  • 3
  • 19
  • 30