Right now Im using helpers to populate select menus in my views. Ex. # Helper def some_options [ ''Option1'', ''Option2'', ''Option3'' ] end # View <%= select :object, :option, some_options %> However I would also like to do my validation on these options as well, making sure nothing is out of bounds. So, my question is where can I place this logic to make it available for validations and views. Should this go in the model? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
If the options aren''t coming from a database table, I would put them in a constant array inside the model, such as: model.rb COUNTRY = [ [ "Canada", "CAN" ], [ "United States", "US" ], [ "England", "UK" ], [ "Other", "OT" ], ].freeze and then just use this array as the source for your select menu. If these select objects are related to your model, you can then validate by using something like: validates_presence_of :country_name or use a custom validation if it''s more complicated. Mike On 9/15/06, Jonathon Marshall <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Right now Im using helpers to populate select menus in my views. > > Ex. > > # Helper > def some_options > [ ''Option1'', ''Option2'', ''Option3'' ] > end > > # View > <%= select :object, :option, some_options %> > > However I would also like to do my validation on these options as well, > making sure nothing is out of bounds. > > So, my question is where can I place this logic to make it available for > validations and views. Should this go in the model? > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Mike, thanks this worked very well. For instance GENDER = [ ''Male'', ''Female'' ].freeze validates_inclusion_of :gender, :in => GENDER, :message => "is invalid" def self.gender_options() GENDER end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---