问题描述
我想添加一个invite_code要求给用户注册。 IE浏览器。除了要求他们指定电子邮件/密码组合外,我还需要一个附加字段:invite_code。这是一个临时解决方案,以便非特定用户在给定的alpha时间段内无法登录。
由于Devise不添加控制器,我感到困惑。我很熟悉虚拟属性的概念,它使我觉得我可以在模型中添加一个:invite_code,然后只是硬编码一个步骤,现在它的邀请代码必须等于12345或现在的任何内容。 / p>
这是否符合设计认证?而且我应该如何从一个适当的轨道平静的方法来接近?
非常感谢。
1)除了getter之外,虚拟属性通常需要一个setter。
最简单的方法是添加
attr_accessor:invite_code
attr_accessible:invite_code#allow invite_code通过质量分配设置
#请参阅下面的James的评论。
到用户模型
2)我认为Devise希望用户模型验证。所以你可以通过添加
validates_each:invite_code,on => :创建do | record,attr,value |
record.errors.add attr,请输入正确的邀请码,除非
value&&值==12345
end
注意:添加:on =>:只有创建新用户才能使用invite_code,而不是进行更新。
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.
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.
这篇关于Ruby on rails:Devise,想添加邀请码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!