首次登录重定向后设计

时间:2014-02-05 09:01:21

标签: ruby-on-rails ruby devise ruby-on-rails-4

通常情况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

当然这不起作用。我错过了什么?

4 个答案:

答案 0 :(得分:17)

经过测试,我们发现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

答案 1 :(得分:0)

用户“user”而不是current_user。

def after_sign_in_path_for(user)
  if user.sign_in_count == 0
    welcome_path
  end
end

答案 2 :(得分:0)

您将'user'作为变量传递,然后您必须使用该变量。并且还给出了else块,因为你正在覆盖这个方法,那么另一个已经登录的用户呢。

def after_sign_in_path_for(user)
  if current_user.sign_in_count == 0
    welcome_path
  else
    #root_path
  end
end

答案 3 :(得分:0)

在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替换为:{/ p>

edit_user_path(current_user)