如何创建默认的“Guest”会话,以便Devise帮助程序current_user将显示我的Guest用户?

时间:2010-11-12 21:11:34

标签: ruby-on-rails authentication ruby-on-rails-3 devise warden

我希望我网站的所有用户都拥有默认的“访客”会话。我正在使用Devise。我的所有设计代码都适用于已注册的用户,但我也有“访客”用户的用户记录。

我想要做的就是自动将某人作为该用户登录,以便在我的视图和其他地方,对Devise的current_user的调用不会失败。

我自9月底以来一直试图找到答案。我甚至无法在Devise邮件列表上得到回复。

1 个答案:

答案 0 :(得分:3)

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