如何在 Rails 3 中创建来宾用户 +设计

发布于 2024-11-16 03:52:51 字数 506 浏览 2 评论 0原文

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

我想启用访客用户。这样,当用户访问某些视图时,就会为该用户创建一个访客用户帐户,然后如果/当他们验证/注册该访客用户时,该访客用户就会升级为普通用户。

将该应用程序视为聊天室。我希望访客用户能够加入聊天室,然后通过 oauth 进入 Twitter 进行注册。但我不想强迫用户在加入之前注册。

建议?

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

问题是我添加了帮助程序,但不明白如何启动 current_or_guest_user。还不确定如何启动每个特定允许的视图?

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

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.

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.

Suggestions?

I found this: https://github.com/plataformatec/devise/wiki/How-To:-Create-a-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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小嗲 2024-11-23 03:52:51

另一种更透明的方式是:

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

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
和我恋爱吧 2024-11-23 03:52:51

遵循 wiki 后,只需将对 current_user 的调用替换为 current_or_guest_user

就视图而言,请在控制器中使用 before_filter 进行操作。

如果没有看到您的任何代码,很难猜测,但在伪代码中,它会类似于

class ChatController < ApplicationController
 before_filter authenticate_user!, :except => [:join]  #this will force users to log in except for the join action

  def join
   chat_handle = current_or_guest_user.handle
   # some more code...
  end 

After following the wiki, just replace calls to current_user with current_or_guest_user

As far as views goes, do it in the controller with a before_filter.

Without seeing any code from you it is hard to guess, but in pseudo-code it would be something like

class ChatController < ApplicationController
 before_filter authenticate_user!, :except => [:join]  #this will force users to log in except for the join action

  def join
   chat_handle = current_or_guest_user.handle
   # some more code...
  end 
再见回来 2024-11-23 03:52:51

查看有关访客用户的此 Rails Cast。 5:40 左右有关 Devise 的详细信息。

Checkout this rails cast about guests users. Details about Devise at around 5:40.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文