首次登录重定向后设计
通常after_sign_up_path
可以工作,但现在我已经confirmations
,这就是垃圾。
我正在寻找一种方法来重定向用户的FIRST SIGN IN
,这意味着
-
sign_in_count == 0
-
last_sign_in == nil
所以我添加到我的applications_controller.rb
def after_sign_in_path_for(user) if current_user.sign_in_count == 0 welcome_path end end
但当然这没用。 我错过了什么?
经过测试,我们发现Devise在登录后立即设置了sign_in_count
的值,这意味着它永远不会为0
,首次登录时它将为1
:
#config/routes.rb devise_for :users, controllers: { sessions: "sessions" } #app/controllers/sessions_controller.rb class SessionController < Devise::DeviseController def after_sign_in_path_for(resource) if resource.sign_in_count == 1 welcome_path else root_path end end end
用户“user”而不是current_user。
def after_sign_in_path_for(user) if user.sign_in_count == 0 welcome_path end end
您将’user’作为变量传递,然后您必须使用该变量。 并且还给出了else块,因为你重写了这个方法,那么另一个已经登录的用户呢。
def after_sign_in_path_for(user) if current_user.sign_in_count == 0 welcome_path else #root_path end end
在ApplicationController(没有设计会话控制器)中,使用Ruby 2.5.1和RoR 5.1.6
def after_sign_in_path_for(user) if user.sign_in_count == 1 first_test_path #see note below else second_test_path end end
我在首次登录时将用户引导至edit_user_path
– 将first_test_path
替换为:
edit_user_path(current_user)