简体   繁体   English

Rails设计错误“没有路由匹配[GET]”/ users / sign_out“”

[英]Rails Devise Error “No route matches [GET] ”/users/sign_out“”

I only get this error when I manually enter the URL into the browser. 当我手动将URL输入浏览器时,我只会收到此错误。 If i click the "sign out" link on my site, it logs the user out fine. 如果我点击我网站上的“退出”链接,它会将用户记录正常。 This is my "sign out" link: 这是我的“退出”链接:

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

which works perfect. 哪作得很完美。 How can I get the sign out functionality working if the user types the address into the address bar? 如果用户在地址栏中键入地址,如何才能使注销功能正常工作? I know it has to do with GET and DELETE requests. 我知道它与GET和DELETE请求有关。 It is using a DELETE request with the link, but using a GET request when the URL is manually entered. 它正在使用链接的DELETE请求,但在手动输入URL时使用GET请求。 How can I fix this? 我怎样才能解决这个问题?

Adding; 添加;

devise_scope :user do get '/users/sign_out' => 'devise/sessions#destroy' end

to the Routes fixed my issue. 路线修复了我的问题。

Modify devise.rb to devise.rb修改为

config.sign_out_via = :get

And change the link 并改变链接

<%= link_to "log out", destroy_user_session_path, method: :get %>

Basically, When you try to type url manually or you try to clickin link "sign-out" with new tab there are HTTP GET request and you will get No route matches [GET] "/users/sign_out" , it doesn't exist because you have only via DELETE request for /users/sign_out on your routes.rb , for the solution you can add this in config/initializer/devise.rb 基本上,当您尝试手动键入url或尝试使用新选项卡单击链接“注销”时,会出现HTTP GET请求,您将获得No route matches [GET] "/users/sign_out" ,它不存在因为你只能通过有DELETE的请求/users/sign_out在你routes.rb ,该解决方案可以在添加此config/initializer/devise.rb

# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = Rails.env.test? ? :get : :delete

And change DELETE to MATCH request on your routes.rb 并在routes.rb上更改DELETEMATCH请求

eg : match 'users/sign_out' => "devise/sessions#destroy" 例如: match 'users/sign_out' => "devise/sessions#destroy"

For reference : Devise for user management and authentication. 供参考: 设计用户管理和身份验证。

To be honest, supposed change the default 'delete' HTTP method it is not recommended. 说实话,假设更改默认的'删除'HTTP方法,建议不要这样做。 It's to user for test (eg using cucumber) Cucumber Testing for “Sign Out” 用户进行测试(例如使用黄瓜) 黄瓜测试用于“退出”

Jose Valim explained why: “GET requests should not change the state of the server. Jose Valim解释了原因:“GET请求不应该改变服务器的状态。 When sign out is a GET request, CSRF can be used to sign you out automatically and things that preload links can eventually sign you out by mistake as well.” 当注销是GET请求时,可以使用CSRF自动注销,并且预先加载链接的内容最终也会错误地将您注销。“

@manoj mona's say 'we must not let the user to sign out by typing in the URL. @manoj mona说'我们不能让用户通过输入网址退出。 It is a bad practice' 这是一个不好的做法'

So, If user to sign out by type in the URL. 因此,如果用户通过URL中的类型注销。

  1. You can to define it, if get request it will redirect or render notice (like stackoverflow sign out) 您可以定义它,如果获取请求它将重定向或呈现通知(如stackoverflow注销)

  2. Or don't use link_to tag for sign out, use input tag with form (like facebook sign out), so users can't type in the URL see this answer 或者不要使用link_to标签进行注销,使用带有表单的input标签(如facebook登出),这样用户就无法输入URL看到这个答案

将其添加到application.js

//= require jquery_ujs 

I have no idea what is the reason (i'm new to ruby on rails), but once you add this line to you applicaiton layout your problem will be solved, 我不知道是什么原因(我是ruby on rails的新手),但是一旦你将这一行添加到应用程序布局中,你的问题就会得到解决,

<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

and keep the logout link as follows 并保持注销链接如下

<%= link_to "Sign Out", destroy_user_session_path, :method => :delete %>

Try this 试试这个

In Routes 在路线

devise_scope :user do
  get '/users/sign_out' => 'sessions#destroy'
end
devise_scope :user do
  match '/users/sign_out' => 'sessions#destroy', via: [:get, :delete]
end

I added ActiveAdmin::Devise.config to my routes.rb. 我将ActiveAdmin::Devise.config添加到我的routes.rb中。 So the full line is, 全线是,

devise_for :users, ActiveAdmin::Devise.config

It's actually in the gem's generator so I believe it's supposed to be in there but I've noticed it doesn't get added in some of my installs. 它实际上是在gem的生成器中,所以我相信它应该在那里,但我注意到它在我的一些安装中没有添加。

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

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