简体   繁体   English

没有路由匹配[GET]“/ logout”?

[英]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 的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? 这是一个JavaScript问题吗? It does not seem to want to recognize method: :delete? 它似乎不想识别方法::删除?

Here is my application.html.erb file: 这是我的application.html.erb文件:

<!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. 呈现的<a>元素不包含要锁定的UJS的data-method属性。 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. 最简单的检查方法是弹出您的Web检查器或查看源并确认相关链接上是否存在该属性。

  2. UJS isn't handling the click on the element. UJS没有处理元素上的点击。 This could be due to a JS error halting execution, but it could also be another click handler you have registered taking precedence. 这可能是由于JS错误停止执行,但它也可能是您已注册的另一个点击处理程序优先。 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: 检查您的JS控制台是否存在错误,以消除这种错误,如果没有,请验证UJS脚本是否位于“sources”面板中:

    在此输入图像描述

    If it's present, and the behavior is the same, try adding a breakpoint on the handleMethod function in UJS and running again. 如果它存在,并且行为相同,请尝试在UJS中的handleMethod函数上添加断点并再次运行。 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. 我不完全理解为什么你认为它是一个JavaScript问题。 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 . 虽然使用破解的javascript运送应用程序并不是一个好主意,但通过使用button_to而不是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 "foo", bar method: deletelink_to "foo", bar method: delete两者实现相同的目标 - 请求作为POST请求发送,使用Rack::MethodOverride使用的特殊_method参数将请求方法设置为补丁,删除等

button_to creates an actual form containing hidden inputs and a single button. button_to创建一个包含隐藏输入和单个按钮的实际表单。 link_to uses javascript (Rails UJS) to create a virtual form and posts the form when the user clicks the link. link_to使用javascript(Rails UJS)创建虚拟表单,并在用户单击链接时发布表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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