Model select and non-model select_tag

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 “%{Please Select…}” kludge I have
above.

I expect there is a good reason for this that I can not see…

On Apr 5, 2:55 pm, John L. [email protected] wrote:

If I want to do the same for a non-model field, logic would dictate

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

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
?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs