本文介绍了无法在 Rails 应用程序中注销,使用 Devise gem,没有路由匹配/users/sign_out的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在不同的网站上有很多关于这个问题的问题.我已经尝试了很多东西,但我仍然无法获得带有 DELETE 方法的链接.

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.

我创建了一个空的 rails 应用程序

I've created an empty rails application

版本:

  • 设计 2.0.4
  • 导轨:3.2.2
  • Ruby:1.9.2
  • RVM:1.10.3

安装 devise gem 后,我运行了以下命令:

After I installed devise gem, I ran these commands:

rails generate devise:install
rails generate devise User

我尝试过的修复如下:

初始链接:

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

我添加了删除方法,有刻度和没有它们:

I added the delete method, in ticks and without them:

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

将此添加到 application.html.erb:

Added this to application.html.erb:

<%= javascript_include_tag :defaults %>

我在 assets/javascripts/application.js 中取消注释这些行:

I uncommented these lines in assets/javascripts/application.js:

= require jquery
= require jquery_ujs

我也尝试让 GET 链接工作,从 config/initializers/devise.rb 更改了这一行

I also tried getting the GET link to work, changed this line from config/initializers/devise.rb

config.sign_out_via = : delete

为此:

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

无论我尝试什么,删除链接都会导致:

No matter what I try, DELETE links result in this:

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

GET 链接也不能正常工作:

GET links don't work as well:

没有路由匹配 [GET] "/"

No route matches [GET] "/"

当然,我试过重新启动服务器.

I have tried restarting the server, of course.

Rake 路由包含以下内容:

Rake routes contains this:

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

提前致谢.

推荐答案

in devise.rb

in devise.rb

find config.sign_out_via = :delete

更改为 config.sign_out_via = :get

如果你使用 :get 确保你使用的是 :method = :get 并且如果你使用 :delete 在你的链接中使用 :method => :delete

if you use :get make sure you are using :method = :get and if you are using :delete using :method => :delete in your link

在生产中你可能需要交换它,所以你应该使用 if 语句来检查 RAILS_ENV

in production you may have to swap it round, so you should use an if statement to check 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.

这篇关于无法在 Rails 应用程序中注销,使用 Devise gem,没有路由匹配/users/sign_out的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:26