本文介绍了Ruby on rails:设计,想添加邀请码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我很困惑,因为 Devise 没有添加控制器.我有点熟悉虚拟属性的概念,让我印象深刻的是我可以在模型中添加一个 :invite_code,然后现在就硬编码一个步骤,它说邀请码必须等于 12345 或其他任何现在.

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.

这对设计身份验证有意义吗?我该如何从适当的 Rails Restful 方法来解决这个问题?

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

非常感谢.

推荐答案

1) 除了 getter 之外,虚拟属性通常还需要一个 setter.

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

最简单的方法是添加

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

到用户模型

2) 我认为 Devise 希望用户模型进行验证.所以你可以通过添加

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

注意:添加 :on => :create 因为invite_code 仅用于创建新用户,而不用于更新.

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

这篇关于Ruby on rails:设计,想添加邀请码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:26