设计注销错误 - 操作“显示”无法找到 UsersController
Devise 生成了以下代码来注销用户:
<%= link_to "Sign out", destroy_user_session_path %>
执行 rake paths
时会出现该路由
destroy_user_session DELETE /users/sign_out(.:format)
{:action=>"destroy", :controller=>"devise/sessions"}
但是我收到一条错误消息:
The action 'show' could not be found for UsersController
有什么想法吗?
Devise generated the following code to sign a user out:
<%= link_to "Sign out", destroy_user_session_path %>
And the route appears when executing rake routes
destroy_user_session DELETE /users/sign_out(.:format)
{:action=>"destroy", :controller=>"devise/sessions"}
However I'm getting an error saying:
The action 'show' could not be found for UsersController
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
路径是正确的,但如果仔细观察,您会发现它不是 GET 请求,而是 DELETE 请求,因此传递方法:
编辑:
这应该向您的链接添加一个
data-method="delete"
属性。验证这一点(查看生成的 HTML)。如果该属性存在,并且单击该链接没有任何反应,请确保您已在布局中包含默认的 javascript 文件。 应该有一行:<%= javascript_include_tag :defaults %>
布局中
。 >>重要提示<<:您不能在地址栏中输入注销 URL 并按 Enter 键,它不起作用,因为它是 GET 而不是 DELETE 请求。这背后的神奇之处在于,JavaScript 帮助程序将挂钩链接的“onclick”事件,然后将隐藏表单(通过 POST)提交到包含隐藏字段的 URL 的
href
目的地_method
值为“delete”。为什么这一切?这是一个安全问题,否则有人可以将您重定向到注销页面,然后简单地注销您,所有未保存的会话内容都会消失...
如果您 reeeeaaaallllyyy 需要通过 GET 注销,那么将其添加到您的
config/initializers 中/devise.rb:
config.sign_out_via
= :get
the path is correct but if you look closely you see that it's not a GET request but a DELETE request so pass the method:
Edit:
This should add a
data-method="delete"
attribute to your link. Verify that (have a look at the generated HTML). If the attribute is present and nothing happens if you click on that link, then ensure that you've included the default javascript files in your layout. There should be a line like:<%= javascript_include_tag :defaults %>
in your layout.
>>Important<<: You can't type the logout url into your address bar and hit enter, it won't work because it's a GET and not a DELETE request. The magic behind this is that a javascript helper will hook on to the "onclick" event of the link and then submit a hidden form (via POST) to the
href
destination of the url containing a hidden field called_method
with the value "delete".Why all this? It's a security thing, otherwise someone could redirect you to the logout page and simply log you out and all your unsaved session stuff will be gone...
If you reeeeaaaallllyyy need a logout via GET then add this to your
config/initializers/devise.rb:
config.sign_out_via = :get
检查以确保您的 application.js 中包含必需的 jquery 作为“//= require jquery”。将其放在 application.js 中的“//= require jquery_ujs”之上。然后重新启动服务器。
Check to make sure that you have required jquery in your application.js as '//= require jquery'. Put it on top of '//= require jquery_ujs' in the application.js. Afterwards restart the server.