How can I create a default Guest session so the Devise current_user helper shows my guest?

I want all users on my site to have a Guest session by default. I am using Devise. All of my development code works for registered users, but I also have a user account for the Guest user.

All I want to do is automatically register someone as this user, so in my views and elsewhere the call to Devise current_user will not end.

I spent since the end of September trying to find the answer to this question. I can’t even get the answer on the Devise mailing list.

+3
source share
1 answer
def set_user
  if current_user.blank?
    user = User.where(:email => "guest@mycompany").first
    if user.blank?
      redirect_to new_user_registration_path
    else
      sign_in(user) # Why was this so hard to find out? I had to read all of the Devise code.
    end
  end
end
+3
source

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


All Articles