[英]Devise After first login redirect
Normally after_sign_up_path
would work but now that i have confirmations
, this goes to the trash. 通常after_sign_up_path
可以工作,但现在我已经confirmations
,这就是垃圾。
I'm searching for a way to redirect a user on his FIRST SIGN IN
, meaning that 我正在寻找一种方法来重定向用户的FIRST SIGN IN
,这意味着
sign_in_count == 0 sign_in_count == 0
last_sign_in == nil last_sign_in == nil
so i added to my applications_controller.rb
所以我添加到我的applications_controller.rb
def after_sign_in_path_for(user)
if current_user.sign_in_count == 0
welcome_path
end
end
but of course this didn't work. 但当然这没用。 What am i missing ? 我错过了什么?
After testing, we found Devise sets the value of sign_in_count
immediately after login, meaning that it's never going to be 0
, it's going to be 1
for a first-time login: 经过测试,我们发现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 "user" instead of current_user. 用户“user”而不是current_user。
def after_sign_in_path_for(user)
if user.sign_in_count == 0
welcome_path
end
end
You passed 'user' as a variable then you have to use that one. 您将'user'作为变量传递,然后您必须使用该变量。 And give else block also because you are overriding this method, then what about the other already logged in user. 并且还给了else块,因为你重写了这个方法,那么另一个已登录的用户呢。
def after_sign_in_path_for(user)
if current_user.sign_in_count == 0
welcome_path
else
#root_path
end
end
In ApplicationController (without devise sessions controller), using Ruby 2.5.1 and RoR 5.1.6 在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
I am directing users to the edit_user_path
on first login - replacing first_test_path
- with: 我在首次登录时将用户引导至edit_user_path
- 将first_test_path
替换为:
edit_user_path(current_user)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.