views:

199

answers:

2

Hi,

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: 

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

Easiest way is to add

attr_accessor :invite_code

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.

Larry K
That's the best way, because you control the validation message.
François Beausoleil
Thank you very much.
Dave
+1  A: 

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

Jeroen
Thanks for the link, but I think this does something slightly different - it allows users to invite other users - wheras for now I simply wish to lock-down sign-up of new users altogether unless they have an invitation code I give out.
Dave