使用Devise登录后,是否可以将用户重定向到不同的页面(基于角色)?它似乎只是重定向到根:to => ... routes.rb中定义的页面
谢谢!
答案 0 :(得分:28)
默认情况下,Devise会在其操作后路由到root。有一篇关于在Devise Wiki上覆盖这些动作的好文章,https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in
或者您可以将stored_locations_for(resource)
设置为nil,然后针对每个操作设置不同的重定向,即:after_sign_up_path(resource)
,after_sign_in_path(resource)
,等等。
答案 1 :(得分:16)
只需将此方法添加到应用程序控制器
即可def after_sign_in_path_for(resource)
user_path(current_user) #your path
end
答案 2 :(得分:0)
Devise具有帮助程序方法(after_sign_in_path_for),可用于覆盖登录/登录后到根的默认Devise路由。
要在登录后实现到另一个路径的重定向,只需将此方法添加到您的应用程序控制器中即可。
#class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
users_path
end
users_path是要重定向到的路径, User 是与Devise模型相对应的模型名称。
N / B:如果您使用 Admin 作为Devise的模型名称,则它将为
#class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
admins_path
end
就是这样。
我希望这对您有帮助
在有帮助的情况下将此答案赞为有用,或在答案下方评论以进一步说明。
答案 3 :(得分:0)
我使用了示例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
答案 4 :(得分:-3)
以下是我认为你正在寻找的设计维基的答案: