[英]Devise/Rails: No route matches [GET] “/users/sign_out”
I'm a bit confused about how devise is routing my requests, for some reason I can't go to the sign-out path in my app now:我对设计如何路由我的请求感到有些困惑,出于某种原因,我现在无法进入我的应用程序中的注销路径:
ActionController::RoutingError (No route matches [GET] "/users/sign_out")
Here is what my routes related to my User model and Devise look like:以下是与我的 User 模型和 Devise 相关的路线:
devise_for :users, :controllers => {:registrations => "registrations"}
devise_scope :user do
get '/settings' => 'registrations#edit'
end
Would defining that scope prevent my other routes from working as well?定义该范围是否也会阻止我的其他路由工作?
Update更新
I don't think that it's supposed to be GET
request, as my link looks like:我不认为它应该是GET
请求,因为我的链接看起来像:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
If you try to go to /users/sign_out by typing it in the address bar of your browser, or linking to it normally, you will get this error.如果您尝试通过在浏览器的地址栏中输入或正常链接来访问 /users/sign_out,您将收到此错误。 To summarize some comments and answers here and from issues at the devise repo on github:总结这里的一些评论和答案以及 github 上设计存储库中的问题:
This is the intended functionality, RESTful apps should not modify state with a GET request.这是预期的功能,RESTful 应用程序不应使用 GET 请求修改状态。
You can fix this by making a link that uses the DELETE method as mentioned by @Trip, @fuzzyalej, and the update by @Joseph Silvashy.您可以通过创建一个使用@Trip、@fuzzyalej 和@Joseph Silvashy 提到的 DELETE 方法的链接来解决此问题。
Alternatively, (and less recommended), in /config/initializers/devise.rb, you could make the following change或者,(不太推荐),在 /config/initializers/devise.rb 中,您可以进行以下更改
config.sign_out_via = :delete
to至
config.sign_out_via = :get
I came across this issue while following along with http://railscasts.com/episodes/209-introducing-devise and with the older version of devise he's using there, this was not an issue.我在关注http://railscasts.com/episodes/209-introducing-devise以及他在那里使用的旧版本设计时遇到了这个问题,这不是问题。
Just so this post has an answer :所以这篇文章有一个答案:
This must be sent with the method, delete
so that it does not default to a get
这必须与方法一起发送, delete
以便它不会默认为get
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
If you're using SSL with Devise controllers, and trying to sign out from an 'http' url, what's happening is it's sending the DELETE request to the #destroy method, but then redirecting to the https version via GET.如果您将 SSL 与 Devise 控制器一起使用,并尝试从“http”网址注销,则发生的情况是它将 DELETE 请求发送到 #destroy 方法,然后通过 GET 重定向到 https 版本。 You can fix this by adding the https to the sign out link like so:您可以通过将 https 添加到注销链接来解决此问题,如下所示:
= link_to "Sign out", destroy_user_session_url(protocol: 'https'), method: :delete
Note: must be 'url' instead of 'path'注意:必须是 'url' 而不是 'path'
Keep your devise.rb
using the correct HTTP method:使用正确的 HTTP 方法保留您的devise.rb
:
# good
config.sign_out_via = :delete
# bad
config.sign_out_via = :get
Use button_to
instead of link_to
使用button_to
而不是link_to
# good
= button_to "Sign Out", destroy_user_session_path, method: :delete
# bad
= link_to "Sign Out", destroy_user_session_path, method: :delete"
If you are using bootstrap (keep it classy)如果您正在使用引导程序(保持优雅)
= link_to "Sign Out", destroy_user_session_path, method: :delete, class: "btn btn-default btn-sm"
From the rails documentation :从Rails 文档:
link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")
# => <a href='http://www.example.com' rel="nofollow" data-method="delete" data-confirm="Are you sure?">Destroy</a>
I had the same problem and to solve it took these steps: 1) include the //= require jquery_ujs in the assets/javascripts/application.js 2) add gem 'jquery-rails' to my gemfile 3) bundle install我遇到了同样的问题,并采取了以下步骤来解决它:1) 在 assets/javascripts/application.js 中包含 //= require jquery_ujs 2) 将 gem 'jquery-rails' 添加到我的 gemfile 3) 捆绑安装
... and it worked. ......它奏效了。
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
in your routes.rb .在你的routes.rb 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.