简体   繁体   English

无法使用Devise gem在Rails应用中注销,没有路线与/ users / sign_out匹配

[英]Unable to sign out in a Rails app, using Devise gem, no route matches /users/sign_out

I know there are dozens of questions on different sites about this issue. 我知道在不同的站点上有很多关于此问题的问题。 I have tried a lot of things, and I still can't get the link with DELETE method to work. 我已经尝试了很多事情,但仍然无法使用DELETE方法进行链接。

I've created an empty rails application 我创建了一个空的Rails应用程序

Versions: 版本:

  • devise 2.0.4 设计2.0.4
  • rails: 3.2.2 导轨:3.2.2
  • Ruby: 1.9.2 红宝石:1.9.2
  • RVM: 1.10.3 RVM:1.10.3

After I installed devise gem, I ran these commands: 安装devise gem之后,我运行了以下命令:

rails generate devise:install
rails generate devise User

Fixes I have tried are below: 我尝试过的修复方法如下:

Initial link: 初始链接:

<%= link_to "Sign out", destroy_user_session_path %>

I added the delete method, in ticks and without them: 我添加了delete方法,但没有刻度:

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

Added this to application.html.erb: 将此添加到application.html.erb:

<%= javascript_include_tag :defaults %>

I uncommented these lines in assets/javascripts/application.js: 我在资产/javascripts/application.js中取消了以下注释:

= require jquery
= require jquery_ujs

I also tried getting the GET link to work, changed this line from config/initializers/devise.rb 我还尝试使GET链接正常工作,从config / initializers / devise.rb更改了这一行

config.sign_out_via = : delete

to this: 对此:

config.sign_out_via = :get if Rails.env.test?

No matter what I try, DELETE links result in this: 不管我尝试什么,DELETE链接都会导致以下结果:

No route matches [GET] "/users/sign_out" 

GET links don't work as well: GET链接无法正常工作:

No route matches [GET] "/" 没有路线匹配[GET]“ /”

I have tried restarting the server, of course. 我当然尝试过重启服务器。

Rake routes contains this: 耙路包含以下内容:

 destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy

Thanks in advance. 提前致谢。

in devise.rb 在devise.rb中

find config.sign_out_via = :delete 找到config.sign_out_via = :delete

change to config.sign_out_via = :get 更改为config.sign_out_via = :get

if you use :get make sure you are using :method = :get and if you are using :delete using :method => :delete in your link 如果使用:get,请确保使用的是:method =:get,如果使用的是:delete,请使用:method =>:delete

in production you may have to swap it round, so you should use an if statement to check RAILS_ENV 在生产中,您可能必须将其交换,因此您应该使用if语句检查RAILS_ENV

In your example you are probably running in development mode and not test, which is why it's probably not working. 在您的示例中,您可能正在开发模式下运行而不是测试,这就是为什么它可能无法正常工作的原因。

And restart your server as it's an initialiser. 并重新启动服务器,因为它是初始化程序。

This helper always works for me: 这个助手总是为我工作:

link_to "Logout", destroy_user_session_path, :method => :delete

Maybe you are not logged in properly? 也许您没有正确登录? You could check with if current_user 您可以检查if current_user

Can we see your routes file? 我们可以看到您的路线文件吗?

Specifically the devise_for call 特别是devise_for调用

You could also try setting the path explicitly 您也可以尝试明确设置路径

devise_for :users, path_names: { sign_out: 'sign_out' }

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

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