如何在 Rails 3 + Devise 中创建访客用户 [英] how to create a guest user in Rails 3 + Devise

查看:22
本文介绍了如何在 Rails 3 + Devise 中创建访客用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个带有设计的 rails 3 应用程序.它需要用户注册/登录才能访问.

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 views a guest user account is created for that user and then if/when they authenticate/register that guest user is then upgraded to a normal user.

将该应用视为聊天室.我希望来宾用户能够加入聊天室,然后 oauth 进入 twitter 进行注册.但我不想强迫用户在加入之前注册.

Think of the app as a chat room. I want guest users to be able to join the chat room, then later oauth into twitter to register. But I don't want to force users to register before joining.

建议?

我发现了这个:https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user

问题是我添加了助手,但不明白如何启动 current_or_guest_user.也不确定如何启动每个特定的允许视图?

Problem with that is I added the helper, but don't understand how to initiate current_or_guest_user. Also not sure how to initiate per specific allowed views?

有什么想法吗?指针?谢谢

Any ideas? Pointers? Thanks

推荐答案

另一种更透明的方式是:

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

这篇关于如何在 Rails 3 + Devise 中创建访客用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆