简体   繁体   中英

link_to with :method => :delete routing through GET not DELETE rails 4

I am making a Ruby on Rails app and have run into a problem which I believe has to do with the Javascript in my app not working.

The specific issue I'm having is with a link_to:

<%= link_to 'Sign out', destroy_user_session_path, :method => :delete %>

Fairly standard as you can see.

I am getting this in my console:

Started GET "/user/sign_out" for ::1 at 2015-06-02

I think this may have something to do with bootstrap and my application.js file but any solution I have found online have no helped.

Here is my application.js file:

//= require bootstrap-sprockets
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

Earlier in my app I was having a similar issue with deleting my "space" entities within the same app, and other similar JS issue so it must be that.

This is on Rails 4 by the way

The Rails Unobtrusive Javascript Driver (UJS) - jquery-rails by default uses the intercepts clicks on links with data-method and sends an AJAX request which either sends the correct HTTP method or fakes it when the client does not support a particular verb by sending a POST request with a special _method parameter.

This is due to the fact that HTML only supports GET actions for links and GET|POST for forms .

You most likely have an issue with your javascript such as a syntax error which is preventing the UJS driver from doing its job. Open up the console in your browser.

Check the whitespaces.

I checked all the comments here. All the includes were set correctly. But I noticed that deleting articles did not work, while deleting comments would work. After rewriting some code and checking here and there, I found two files that looked identically in the editor, but one version would work and one would not work!

curry-blog/app/views/articles/index.html.erb:

<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th colspan="3"></th>
  </tr>
 
  <% @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 'Delete', article_path(article),
              method: :delete,
              data: { confirm: 'Are you sure?' } %></td>        
    </tr>
  <% end %>
</table>

However, looking at the files with xxdiff I found that in one version only tabs where used, while the other also used blanks. Probably, from copy pasting code from the tutorial. Replacing the blanks with tabs did therefore fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM