Ruby on Rails:设计,想要添加邀请代码吗?

发布于 2024-09-18 14:41:50 字数 285 浏览 2 评论 0原文

我想为用户注册添加一个邀请码要求。 IE。除了要求他们指定电子邮件/密码组合之外,我还想要一个附加字段:invite_code。这是一个临时修复,以便不需要的用户无法在给定的 alpha 期间登录。

我很困惑,因为 Devise 不添加控制器。我对虚拟属性的概念有点熟悉,我突然想到我可以向模型添加一个 :invite_code ,然后现在就硬编码一个步骤,其中指示邀请代码必须等于 12345 或目前的其他值。

这对于设备身份验证有意义吗?我该如何从正确的 Rails 宁静方法来解决这个问题?

非常感谢。

I would like to add an invite_code requirement for users to sign up. Ie. in addition to requiring them to specify an email/password combo, I want an additional field :invite_code. This is a temporary fix so that non-wanted users cannot login during a given alpha period.

I'm confused since Devise doesn't add controllers. I'm sort of familiar with the concept of virtual attributes, and it strikes me that I could add a :invite_code to the model, and then just hard code a step now where it says invite code must equal 12345 or whatever for now.

Does this make sense with devise authentication? And how do I go approaching this from a proper rails restful approach?

Thank you very much.

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

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

发布评论

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

评论(3

离鸿 2024-09-25 14:41:50

1) 虚拟属性除了 getter 之外通常还需要 setter。

最简单的方法是添加

attr_accessor :invite_code
attr_accessible :invite_code # allow invite_code to be set via mass-assignment
    # See comment by James, below.

到用户模型

2) 我认为 Devise 希望用户模型进行验证。因此,您可以通过添加注释来停止验证

validates_each :invite_code, :on => :create do |record, attr, value|
    record.errors.add attr, "Please enter correct invite code" unless
      value && value == "12345"
end

:added :on => :create 因为邀请代码仅用于创建新用户,而不用于更新。

1) A virtual attribute usually needs a setter in addition to a getter.

Easiest way is to add

attr_accessor :invite_code
attr_accessible :invite_code # allow invite_code to be set via mass-assignment
    # See comment by James, below.

to the User model

2) I presume that Devise wants the User model to validate. So you could stop the validation by adding

validates_each :invite_code, :on => :create do |record, attr, value|
    record.errors.add attr, "Please enter correct invite code" unless
      value && value == "12345"
end

NOTE: added :on => :create since the invite_code is only needed for creating the new user, not for updating.

忘年祭陌 2024-09-25 14:41:50

试试这个:http://github.com/scambra/devise_invitable

它添加了支持 devise 用于通过电子邮件发送邀请(需要经过身份验证)并接受邀请设置密码。

它适用于 Devise >= 4.0 如果您想使用 devise 3.0.x,则必须使用 1.2.1 或更低版本 如果您想使用 devise 3.1.x,则必须使用 1.3.2 或更低版本 如果您想要要使用 devise >= 3.2,您必须使用 1.6.1 或更低版本...

Try this: http://github.com/scambra/devise_invitable

It adds support to devise for sending invitations by email (it requires to be authenticated) and accept the invitation setting the password.

It works with Devise >= 4.0 If you want to use devise 3.0.x, you must use 1.2.1 or lower If you want to use devise 3.1.x, you must use 1.3.2 or lower If you want to use devise >= 3.2, you must use 1.6.1 or lower...

坏尐絯 2024-09-25 14:41:50

根据文档,invitable 可以让你控制谁可以邀请其他人。如果 invitation_limit 设置为“0”,则人们无法分发邀请。

来自文档:

invitation_limit:用户可以发送的邀请数量。这
默认值 nil 意味着用户可以发送尽可能多的邀请
想要,对任何用户都没有限制,invitation_limit列不是
用过的。设置为 0 意味着他们无法发送邀请。设定n>1
0 表示他们可以发送 n 个邀请。您可以更改invitation_limit
某些用户的列,以便他们可以发送更多或更少的邀请,甚至
全局邀请限制 = 0。

According to the docs, invitable gives you control over who gets to invites others. People cannot distribute invites if there is a "0" setting for invitation_limit.

From the docs:

invitation_limit: The number of invitations users can send. The
default value of nil means users can send as many invites as they
want, there is no limit for any user, invitation_limit column is not
used. A setting of 0 means they can't send invitations. A setting n >
0 means they can send n invitations. You can change invitation_limit
column for some users so they can send more or less invitations, even
with global invitation_limit = 0.

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