Q. What resource do readers recommend for referencing regarding best practices for form construction in Rails? I have the following situation: location_type of Locations may have one of five (5) possible codes. Presently I have these codes listed in a drop-down select constructed thusly: <p> <b>Location type</b><br /> <%= f.select :location_type, [ [''MAIN - Main Address'', ''MAIN''], [''POST - Postal Delivery'', ''POST''], [''DELV - Package Delivery'', ''DELV''], [''SHIP - Package Shipping'', ''SHIP''], [''OTHR - Other'', ''OTHR''], ], :selected => ''MAIN'', :size => 4 -%> </p> Should this instead be constructed so that the model, Locations, has these virtual attributes: valid_location_type[] [ [''MAIN - Main Location'',''MAIN''], [''POST - Postal Delivery'', ''POST''], ... ] default_location_type = ''MAIN'' and then the view could have this instead: <%= f.select :location_type, @location.valid_location_type, :selected => @location.default_location_type, :size => 4 Would this even work? Comments? -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 5, 7:31 pm, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Should this instead be constructed so that the model, Locations, has > these virtual attributes: > > valid_location_type[]> [ > [''MAIN - Main Location'',''MAIN''], > [''POST - Postal Delivery'', ''POST''], > ... > ] > > default_location_type = ''MAIN'' >Personally I would have it as a constant of the Location class, ie class Location ... VALID_TYPES = [...] end Fred> and then the view could have this instead: > > <%= f.select :location_type, > @location.valid_location_type, > :selected => @location.default_location_type, > :size => 4 > > Would this even work? > > Comments? > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Nov 5, 7:31�pm, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> � � default_location_type = ''MAIN'' >> > Personally I would have it as a constant of the Location class, ie > > class Location ... > VALID_TYPES = [...] > end > > FredPoint taken. Nonetheless, in the interim I have tested it and this does indeed work as expected. However, I used private methods and public getters to implement. def valid_location_type virt_valid_type end private def virt_valid_type vvt [ [''MAIN - Main Address'', ''MAIN''], [''POST - Postal Delivery'', ''POST''], [''DELV - Package Delivery'', ''DELV''], [''SHIP - Package Shipping'', ''SHIP''], [''OTHR - Other'', ''OTHR''] ] end But my question remains: Is this the preferred idiom or is there another recommended way to do this? -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Looks like a good place to give this a spin: http://railscasts.com/episodes/121-non-active-record-model Basically, use a pure-ruby class to wrap the location types and let it support an ActiveRecord like interface (mainly find and all). Then you can do collection_select the way you would with a normal ARec model. Depending on which version of Rails you''re working with you may also check into PassiveRecord. It''s a pretty advanced version of the same concept. Unfortunately it has dependencies on older versions of ARec. On Nov 5, 3:02 pm, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote: > > On Nov 5, 7:31 pm, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > >> default_location_type = ''MAIN'' > > > Personally I would have it as a constant of the Location class, ie > > > class Location ... > > VALID_TYPES = [...] > > end > > > Fred > > Point taken. > > Nonetheless, in the interim I have tested it and this does indeed work > as expected. However, I used private methods and public getters to > implement. > > def valid_location_type > virt_valid_type > end > > private > > def virt_valid_type > vvt > [ > [''MAIN - Main Address'', ''MAIN''], > [''POST - Postal Delivery'', ''POST''], > [''DELV - Package Delivery'', ''DELV''], > [''SHIP - Package Shipping'', ''SHIP''], > [''OTHR - Other'', ''OTHR''] > ] > end > > But my question remains: Is this the preferred idiom or is there > another recommended way to do this? > -- > Posted viahttp://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?hl=en -~----------~----~----~----~------~----~------~--~---
I think Fred''s given the preferred idiom. It''s simple & it works. -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of James Byrne Sent: Wednesday, November 05, 2008 12:03 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: select in form - best practice? Frederick Cheung wrote:> On Nov 5, 7:31 pm, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> default_location_type = ''MAIN'' >> > Personally I would have it as a constant of the Location class, ie > > class Location ... > VALID_TYPES = [...] > end > > FredPoint taken. Nonetheless, in the interim I have tested it and this does indeed work as expected. However, I used private methods and public getters to implement. def valid_location_type virt_valid_type end private def virt_valid_type vvt [ [''MAIN - Main Address'', ''MAIN''], [''POST - Postal Delivery'', ''POST''], [''DELV - Package Delivery'', ''DELV''], [''SHIP - Package Shipping'', ''SHIP''], [''OTHR - Other'', ''OTHR''] ] end But my question remains: Is this the preferred idiom or is there another recommended way to do this? -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---