Develop After the first login redirect

It after_sign_up_pathwill usually work, but now that I have it confirmations, this applies to the basket.

I am looking for a way to redirect the user to him FIRST SIGN IN, which means

  • sign_in_count == 0

  • last_sign_in == nil

so I added to my applications_controller.rb

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

but of course it didn’t work. What am I missing?

+4
source share
4 answers

After testing, we found that Devise sets the value sign_in_countimmediately after logging in, which means that it will never be 0, it will be 1for logging in:

#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
+17
source

"" current_user.

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

You passed "user" as a variable, then you should use it. And give another block also because you are redefining this method, and then about another already registered user.

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

In ApplicationController (without development session controller) using Ruby 2.5.1 and 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 direct users to the edit_user_pathfirst time I log in - replacing first_test_path- with:

edit_user_path(current_user)
0
source

Source: https://habr.com/ru/post/1525378/


All Articles