Hello everyone, I''m just trying to understand some logical reasons behind Rails syntax of "select" and "select_tag". I know I can do this: <%= form.select :card_type, supported_card_types, { :prompt => "Please Select..."} %> which sets a model field in the params hash. If I want to do the same for a non-model field, logic would dictate something like: <%= select :card_type, supported_card_types, { :prompt => "Please Select..."} %> However what is required is like this: <%= select_tag :card_type, %{<option value="">Please Select...</option>} + options_for_select(supported_card_types) %> It seems very odd to me that select_tag can not follow the same (and elegent) syntax of select so model and non-model fields can be coded the same way. It seems particularly odd that the latter requires use of "options_for_select" and does not support :html_options. The lack of the :html_options means you can not use :prompt so have to resort to the nasty "%{<option value="">Please Select...</option>}" kludge I have above. I expect there is a good reason for this that I can not see... -- 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 Apr 5, 2:55 pm, John Lane <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello everyone, I''m just trying to understand some logical reasons > behind Rails syntax of "select" and "select_tag". > > I know I can do this: > > <%= form.select :card_type, supported_card_types, > { :prompt => "Please Select..."} %> > > which sets a model field in the params hash. > > If I want to do the same for a non-model field, logic would dictate > something like: > > <%= select :card_type, supported_card_types, > { :prompt => "Please Select..."} %> > > However what is required is like this: > > <%= select_tag :card_type, > %{<option value="">Please Select...</option>} + > options_for_select(supported_card_types) %> > > It seems very odd to me that select_tag can not follow the same (and > elegent) syntax of select so model and non-model fields can be coded the > same way. >There''s a bit of history here. Before rails 1.2 there was no form_for, and so helpers split into two families: select, text_field, check_box etc... who all took as there first argument the name of an instance variable and as their second the name of a method (note that this never had to correspond to an actual database attribute) and select_tag, text_field_tag etc... who just generated the appropriate tag given the name and the value for when what you were doing didn''t fit that pattern. They aren''t as smart because they make fewer assumptions about what you are trying to do. form_for allowed you to stop repeating that first parameter for each field in the form and also allow escaping the convention of the instance variable. Fred> It seems particularly odd that the latter requires use of > "options_for_select" and does not support :html_options. The lack of the > :html_options means you can not use :prompt so have to resort to the > nasty "%{<option value="">Please Select...</option>}" kludge I have > above. > > I expect there is a good reason for this that I can not see... > -- > 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 -~----------~----~----~----~------~----~------~--~---
Having looked into non-model fields there are also inconsistencies between the various non-model fields. For instance, we have select_tag and text_field_tag but for dates it is date_select rather than date_select_tag as one would expect. It seems to me that it would make lots more sense if there was a consistent model and non-model set of field helpers that took similar arguments. ie, model field: <%= form.select :card_type, supported_card_types, { :prompt => "Please Select..."} %> and non-model field: <%= select :card_type, supported_card_types, { :prompt => "Please Select..."} %> Something perhaps that could be considered for a future version of Rails ? -- 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 -~----------~----~----~----~------~----~------~--~---