简体   繁体   中英

no route matches [GET] “/logout”?

Rails.application.routes.draw do
  root to: 'users#index'
  get    '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  resources :users
end

<% if logged_in? %>
    <li><%= link_to "Sign out", logout_path, method: :delete %>
<% end %>

GemFile

gem 'jquery-rails'
gem 'rails', '4.2.2'
gem 'turbolinks'


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

Is this a javascript issue? It does not seem to want to recognize method: :delete?

Here is my application.html.erb file:

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

<%= yield %>

</body>
</html>

Is there an alternative way to pass method: :delete to the route?

One of two things is happening:

  1. The <a> element rendered doesn't contain the data-method attribute for UJS to latch onto. Perhaps there's a stale version cached or you're looking at a different view? Easiest way to check is to pop into your web inspector or view source and confirm the presence of the attribute on the relevant link.

  2. UJS isn't handling the click on the element. This could be due to a JS error halting execution, but it could also be another click handler you have registered taking precedence. Check your JS console for errors to eliminate that as a potential cause, and if not, verify that the UJS script is in your “sources” panel:

    在此输入图像描述

    If it's present, and the behavior is the same, try adding a breakpoint on the handleMethod function in UJS and running again. If it's triggered, step through to see what happens that's resulting in failure. If it's not triggered, you can back up from there and look at other handlers on the link.

    [ 断点 ]

I don't fully understand why you believe it to be a javascript issue. I think you need something like the following:

<% if logged_in? %>
    <li>
        <%= link_to logout_path, method: :delete do %>
            <i class="fa fa-power-off"></i> Sign Out
        <% end %>
    </li>
<% end %>

While shipping apps with broken javascript is not a good idea it can make sense to harden certain critical aspects of your app like logout by using button_to instead of link_to .

button_to "foo", bar method: delete and link_to "foo", bar method: delete both achieve the same goal - the request is sent as a POST request with the special _method param used by Rack::MethodOverride to set the request method to PATCH, DELETE etc.

button_to creates an actual form containing hidden inputs and a single button. link_to uses javascript (Rails UJS) to create a virtual form and posts the form when the user clicks the link.

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