how to create a guest user in Rails 3 + Devise

前端 未结 3 1624
闹比i
闹比i 2021-01-31 23:22

currently I have a rails 3 app with devise. it requires the users to register / signin for access.

I would like to enable guest users. So that when a user visits certain

3条回答
  •  醉梦人生
    2021-02-01 00:00

    Another more transparent way is this:

    def current_user
      super || guest_user
    end
    
    private
    
    def guest_user
     User.find(session[:guest_user_id].nil? ? session[:guest_user_id] = create_guest_user.id : session[:guest_user_id])
    end
    
    def create_guest_user
      u = User.create(:name => "guest", :email => "guest_#{Time.now.to_i}#{rand(99)}@example.com")
      u.save(:validate => false)
      u
    end
    

提交回复
热议问题