ruby-on-railsauthenticationruby-on-rails-3devisewarden

How can I create a default "Guest" session so that the Devise helper current_user will show my Guest user?


I want all users to my site to have a default "Guest" session. I am using Devise. All of my devise code is working for signed up users, but I also have a user record for a "Guest" user.

All I want to do is automatically log someone in as that user, so that in my views and elsewhere, the call to Devise's current_user will not fail.

I have spent since the end of September trying to find an answer for this. I can't even get a reply on the Devise mailing list.


Solution

  • 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