Ruby-on-rails 设计:为什么我的注销链接不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6946435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Devise: Why doesn't my logout link work?
提问by jaydel
the problem: In a nutshell, when I try to install a logout link to my app it fails to work. Here's as much context as I can think to put here (if you want anything else, please poke me)...
问题:简而言之,当我尝试将注销链接安装到我的应用程序时,它无法正常工作。这是我能想到的尽可能多的上下文(如果你想要其他任何东西,请戳我)......
I've got this in a haml view:
我在haml视图中得到了这个:
= link_to("Logout", destroy_user_session_path, :method => :delete)
It generates this in the view:
它在视图中生成:
<a href="/users/sign_out" data-method="delete" rel="nofollow">Logout</a>
I verified that in my config/initializers/devise.rb I have this setting uncommented and correct:
我确认在我的 config/initializers/devise.rb 中我有这个设置没有注释和正确:
config.sign_out_via = :delete
I validated the following route:
我验证了以下路线:
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
I also have this bit of trickery in my routes.rb, and I suspect this is related to my issue:
我的routes.rb中也有这个小技巧,我怀疑这与我的问题有关:
devise_for :users, :controllers => {:sessions => "devise/sessions", :registrations => "users"}
resources :users
This last bit is because I want to manage (edit, create and delete) users in my own controller.
最后一点是因为我想在我自己的控制器中管理(编辑、创建和删除)用户。
The error message I'm getting is as follows:
我收到的错误消息如下:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with ID=sign_out
Rails.root: /home/jaydel/projects/mbsquared-projects/Wilson-Goldrick
app/controllers/users_controller.rb:16:in `show'
In my server logs I see this for the request:
在我的服务器日志中,我看到了这个请求:
Started GET "/users/sign_out" for 127.0.0.1 at 2011-08-04 13:08:51 -0500
Processing by UsersController#show as HTML
Parameters: {"id"=>"sign_out"}
Anyone have any ideas?
谁有想法?
采纳答案by Olives
The problem lies in the fact that in your logs the signout request is a GET request.
问题在于,在您的日志中,注销请求是一个 GET 请求。
Started GET "/users/sign_out"
But the signout route is a DELETE
但是注销路由是 DELETE
destroy_user_session DELETE /users/sign_out(.:format)
The reason why you are getting the exception is that is it getting confused with one of the routes created by resources :userswhich would be something like
您收到异常的原因是它与创建的路由之一混淆,resources :users类似于
edit_user GET /users/(:id)(.:format) {:action=>"edit", :controller=>"users"}
Basically 'sign_out' is being mistaken as a id.
基本上'sign_out'被误认为是一个id。
I'm not sure why the delete link is not going through as a DELETE request. Though changing
我不确定为什么删除链接没有作为 DELETE 请求通过。虽然在变
config.sign_out_via = :delete
to be :getmight solve the problem.
是:get可以解决的问题。
回答by Jay Beale
The correct way to fix this, REST-wise, would be to change your logout links to use the DELETE method. It's a very easy fix, changing this:
解决这个问题的正确方法,REST-wise,是更改您的注销链接以使用 DELETE 方法。这是一个非常简单的修复,改变这个:
link_to "Log out", destroy_user_session_path
to this:
对此:
link_to "Log out", destroy_user_session_path, :method => :delete
回答by ka8725
I had the same problem with rails 3.2 when I deleted from application.jsthis line:
当我application.js从这一行中删除时,rails 3.2 也遇到了同样的问题:
//= require jquery_ujs
So, I think you have to insert this line in your application.jsif you haven't it there.
所以,我认为application.js如果你没有这一行,你必须在那里插入这一行。
PS. This behavior means that rails adapter for jquerydoesn't function. So you should make sure if it is loaded in your html in browser. You should test it in development mode because you will have compressed js in production and it will be very difficult to find something there.
附注。这种行为意味着 rails 适配器jquery不起作用。因此,您应该确保它是否已加载到浏览器的 html 中。你应该在开发模式下测试它,因为你将在生产中压缩 js,并且很难在那里找到一些东西。
回答by webgen
You need bothlink_to "Log out", destroy_user_session_path, method: :delete
你需要两者link_to "Log out", destroy_user_session_path, method: :delete
&& these included in application.js
&& 这些包含在 application.js 中
//= require jquery
//= require jquery_ujs
. Obviously, don't forget to add this to Gemfile if for some reason it is not there.
. 显然,如果由于某种原因它不存在,请不要忘记将其添加到 Gemfile 中。
gem 'jquery-rails'
gem 'jquery-rails'
回答by pws5068
This is an old question but the accepted answer is not the proper way to solve this issue (although it is a good diagnosis of the problem).
这是一个古老的问题,但公认的答案并不是解决这个问题的正确方法(尽管它是对问题的一个很好的诊断)。
When I ran into this today my routes file appeared as:
当我今天遇到这个时,我的路由文件显示为:
root :to => 'welcome#index'
resources :users
devise_for :users
As mentioned by Olives the resources statement is simply causing /users/anything to trip up. I simply needed to reverse the ordering of my routes...
正如 Olives 所提到的,resources 语句只是导致 /users/anything 出错。我只需要颠倒我的路线的顺序......
root :to => 'welcome#index'
devise_for :users
resources :users
回答by Christoph
jQuery is required in order to perform the request with DELETE. Make sure you didn't drop it from your application.js.
需要 jQuery 才能使用 DELETE 执行请求。确保您没有从 application.js 中删除它。
回答by Ross Allen
This is an old question, but I wanted a way that required no JavaScript. Rails enables "DELETE" like requests with a hidden input that sets methodto delete. Use a real <form>, and let Rails generate the necessary hidden inputs:
这是一个老问题,但我想要一种不需要 JavaScript 的方法。Rails 启用“删除”之类的请求,其中隐藏输入设置method为delete. 使用一个 real <form>,并让 Rails 生成必要的隐藏输入:
<%= form_for(resource, as: resource_name,
url: destroy_user_session_path, html: { method: :delete }) do |f| %>
<%= f.submit "Log out" %>
<% end %>
the above generates something like:
上面会产生类似的东西:
<form accept-charset="UTF-8" action="/users/sign_out" method="post">
<div style="display:none">
<input name="utf8" type="hidden" value="?">
<input name="_method" type="hidden" value="delete">
<input name="authenticity_token" type="hidden" value="1234">
</div>
<input name="commit" type="submit" value="Log out">
</form>
回答by RafeekPro
I've checked all this solutions, but no one has worked for me. Finally I found that in header I deleted two lines including application tags and csrf meta
我已经检查了所有这些解决方案,但没有人对我来说有效。最后我发现在标题中我删除了两行,包括应用程序标签和 csrf meta
So, If someone have this problem with no sollution at this point - check if you have in header of the page (HAML notation)
所以,如果有人在这一点上没有解决这个问题 - 检查你是否有页面标题(HAML 表示法)
= javascript_include_tag 'application'
= csrf_meta_tags
回答by user212131
You also have to check, is resources :usersunder devise_for :usersand is the link method delete
你还得检查resources :users下devise_for :users,是不是链接方法删除
link_to "Logout", destroy_user_session_path, method: :delete
if both are right, your logout button should work
如果两者都正确,您的注销按钮应该可以工作
回答by ScottJShea
I had to go about this in a different manner as my application.rb had:
我不得不以与我的 application.rb 不同的方式进行处理:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
and was still failing. It turns out I needed to set config.serve_static_assets = truein /environment/development, test and production.rb. This linkwas what helped me figure it out.
并且仍然失败。事实证明我需要config.serve_static_assets = true在 /environment/development、test 和 production.rb 中进行设置。这个链接帮助我弄清楚了。

