Ruby-on-rails 如何设计销毁会话并从控制器注销?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25858606/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:54:59  来源:igfitidea点击:

How to devise destroy session and sign out from controller?

ruby-on-railsrubyruby-on-rails-4devise

提问by fabbb

Devise destroy session and sign out from controller?

设计销毁会话并从控制器注销?

if something_is_not_kosher
  # 1. log this event, 2. send notice
  redirect_to destroy_user_session_path and return
end

Also tried:

还试过:

if something_is_not_kosher
 # 1. log this event, 2. send notice
  redirect_to controller: 'devise/sessions', action: 'destroy', method: :delete and return
end

Error is No route matches [GET] "/users/sign_out"but I'm explicitly setting method: :delete in example 2. Maybe devise has a method? current_user.sign_outand tried sign_out(current_user) which also don't work? Thanks for the help.

错误是,No route matches [GET] "/users/sign_out"但我在示例 2 中明确设置了方法::delete。也许设计有一个方法?current_user.sign_out并尝试了 sign_out(current_user) 这也不起作用?谢谢您的帮助。

rake routes:

耙路线:

        new_user_session GET    /users/sign_in(.:format)         devise/sessions#new
            user_session POST   /users/sign_in(.:format)         devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)        devise/sessions#destroy
           user_password POST   /users/password(.:format)        devise/passwords#create
       new_user_password GET    /users/password/new(.:format)    devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)   devise/passwords#edit
                         PATCH  /users/password(.:format)        devise/passwords#update
                         PUT    /users/password(.:format)        devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)          users/registrations#cancel
       user_registration POST   /users(.:format)                 users/registrations#create
   new_user_registration GET    /users/sign_up(.:format)         users/registrations#new
  edit_user_registration GET    /users/edit(.:format)            users/registrations#edit
                         PATCH  /users(.:format)                 users/registrations#update
                         PUT    /users(.:format)                 users/registrations#update
                         DELETE /users(.:format)                 users/registrations#destroy

采纳答案by fabbb

So I ended up solving this by creating a custom signout route

所以我最终通过创建自定义注销路由来解决这个问题

  devise_scope :user do
    get '/signout', to: 'devise/sessions#destroy', as: :signout
  end

and in my controller I have:

在我的控制器中,我有:

if something_is_not_kosher
  redirect_to signout_path and return
end

回答by AOG

Why don't you just use devise's built-in sign_out_and_redirect(current_user)method?

为什么不直接使用 devise 的内置sign_out_and_redirect(current_user)方法?

回答by dddd1919

destroy_user_session_path(@user)is sign out path for user, but it must requst with DELETEmethod. redirect_tomethod will tell broswer to request another path, but broswer just can request with GETmethod.
So if you want to let user to sign out, you must set a sign out form with DELETEmethod or with AJAX request to let user sign out but not with redirect_tofunction.

destroy_user_session_path(@user)是用户的退出路径,但必须使用DELETE方法。redirect_to方法会告诉浏览器请求另一条路径,但浏览器只能用GET方法请求。
所以如果你想让用户退出,你必须设置一个带有DELETE方法或AJAX请求的退出表单,让用户退出而不是redirect_to功能。

If you just want to destroy user session, use sign_out @useris ok.

如果你只是想销毁用户会话,使用sign_out @user是可以的。

回答by William Hu

Just in case someone can't use it directly.

以防万一有人不能直接使用它。

        <% if user_signed_in? %>
        <li><%= link_to "Logout", destroy_user_session_path, :method => :delete %></li>
        <% else %>
        <li><%= link_to "Sign up now!",  new_user_registration_path%></li>
        <% end %>