Hi everybody, I am creating a site, where people can register, but i only want them to register, if they have a legit code. So let''s say I give out the three codes "code1", "code2" and "code3". How can I make sure, that the user only gets registered, when the code gets vaildated? Validate :registration_code => presence => true, ??? => ??? Do I have to write a custom method where I define all the possible codes or how would you do it? Thanks for your help! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Sep 14, 2011 at 6:02 PM, Thomas <schneiderinho-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>wrote:> I am creating a site, where people can register, but i only want them > to register, if they have a legit code. So let''s say I give out the > three codes "code1", "code2" and "code3". How can I make sure, that > the user only gets registered, when the code gets vaildated? > > Validate :registration_code => presence => true, ??? => ??? > > Do I have to write a custom method where I define all the possible > codes or how would you do it? >Probably "validates_inclusion_of" can help you. HTH, Peter -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
The easiest way is probably to just define a custom method and use that for
validation:
validate :registration_code_must_be_valid
def registration_code_must_be_valid
# check if it''s one of the hard coded values
unless [ ''code1'', ''code2''
].include?(registration_code)
errors.add(:registration_code, "must be valid")
end
# or check if it''s in another model if you want to do it dynamically
unless RegistrationCode.find_by_code(registration_code)
errors.add(:registration_code, "must be valid")
end
end
Another option is to create a custom validator. Check out that section of
the guide:
http://guides.rubyonrails.org/active_record_validations_callbacks.html#custom-validators
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/GFJlCx7MCt8J.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.