Ruby-on-rails Rails:使用 Devise 登录后重定向

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

Rails: Redirect after sign in with Devise

ruby-on-railsdevise

提问by rsl

Is it possible to redirect users to different pages (based on role) after signing in with Devise? It only seems to redirect to the root :to => ... page defined in routes.rb

使用 Devise 登录后,是否可以将用户重定向到不同的页面(基于角色)?它似乎只重定向到 root :to => ... 在 routes.rb 中定义的页面

Thanks!

谢谢!

回答by janders223

By default Devise does route to root after it's actions. There is a nice article about overriding these actions on the Devise Wiki, https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in

默认情况下,Devise 会在其操作后路由到 root。Devise Wiki 上有一篇关于覆盖这些操作的好文章,https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in

Or you can go even farther by setting stored_locations_for(resource)to nil, and then have different redirects for each action, ie: after_sign_up_path(resource), after_sign_in_path(resource)and so on.

或者你也可以通过设置走得更远stored_locations_for(resource)到零,然后对每个动作的不同重定向,即:after_sign_up_path(resource)after_sign_in_path(resource)等等。

回答by Asnad Atta

simply you can add this method in to your application controller

只需将此方法添加到应用程序控制器中

def after_sign_in_path_for(resource)
  user_path(current_user) #your path
end

回答by Anoob K Bava

only paste the below code to the application controller or any controller , you need to do the operation;

只需将以下代码粘贴到应用程序控制器或任何控制器,您就需要进行操作;

def after_sign_in_path_for(resource)
    users_path
end

回答by Promise Preston

Devise has a helper method after_sign_in_path_forwhich can be used to override the default Devise route to root after login/sign-in.

Devise 有一个辅助方法after_sign_in_path_for,可用于在登录/登录后覆盖默认的 Devise 路由到 root。

To implement a redirect to another path after login, simply add this method to your application controller.

要在登录后实现重定向到另一个路径,只需将此方法添加到您的应用程序控制器。

#class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
  users_path
end

Where users_path is the path that you want it to redirect to, Useris the model name that corresponds to the model for Devise.

其中 users_path 是您希望它重定向到的路径,User是与 Devise 模型对应的模型名称。

N/B: If you used Adminas your model name for Devise, then it will be

N/B:如果您使用Admin作为 Devise 的模型名称,那么它将是

#class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
  admins_path
end

That's all.

就这样。

I hope this helps

我希望这有帮助

回答by shilovk

https://github.com/heartcombo/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in,-sign-up,-or-sign-out

https://github.com/heartcombo/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in,-sign-up,-or-sign-out

I used example 1:

我使用了示例 1:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :authenticate_user!

  protected

  def after_sign_in_path_for(resource)
    current_user.is_a?(Admin) ? admin_tests_path : (stored_location_for(resource) || root_path)
  end
end

回答by Kevin Bedell

Here's what I believe is the answer you are looking for from the devise wiki:

我相信这是您从设计维基中寻找的答案:

How To: Change the default sign_in and sign_out routes

如何:更改默认的 sign_in 和 sign_out 路由