radio_button_tag for every value of constant array

How do I create a radio_button_tag for every value of a constant
array? E.g., I have this constant array: VALID_ORIENTATIONS =
["Straight", "Gay", "Bisexual"] . A user is presented with a form to
search for other users according to sexual orientation. Currently,
the form looks like this:

<div class="form_row">
    <label for="orientation">Sexual Orientation:</label>
    <%= radio_button_tag :orientation, "Straight",
    params[:orientation] == 'Straight',
    :id => "Straight" %>Straight
    <%= radio_button_tag :orientation, "Gay",
    params[:orientation] == 'Gay',
    :id => "Gay" %>Gay
    <%= radio_button_tag :orientation, "Bisexual",
    params[:orientation] == 'Bisexual',
    :id => "Bisexual" %>Bisexual
    <%= radio_button_tag :orientation, "",
    params[:orientation].blank?,
    :id => "Any" %>Any
  </div>

This is not DRY. How can I iterate through every value in the
constant array to make these radio buttons? Is partial appropriate
for this? The array has only three values.

Also: the blank in the last radio button is obviously not in the
array. It's there to allow the user to not use this search
criterion. Is there a better way for a user to unselect a radio
button without choosing any other?

Thanks.

Learn by Doing wrote:

How do I create a radio_button_tag for every value of a constant
array? E.g., I have this constant array: VALID_ORIENTATIONS =
["Straight", "Gay", "Bisexual"] . A user is presented with a form to
search for other users according to sexual orientation. Currently,
the form looks like this:

<div class="form_row">
    <label for="orientation">Sexual Orientation:</label>
    <%= radio_button_tag :orientation, "Straight",
    params[:orientation] == 'Straight',
    :id => "Straight" %>Straight
    <%= radio_button_tag :orientation, "Gay",
    params[:orientation] == 'Gay',
    :id => "Gay" %>Gay
    <%= radio_button_tag :orientation, "Bisexual",
    params[:orientation] == 'Bisexual',
    :id => "Bisexual" %>Bisexual
    <%= radio_button_tag :orientation, "",
    params[:orientation].blank?,
    :id => "Any" %>Any
  </div>

This is not DRY. How can I iterate through every value in the
constant array to make these radio buttons? Is partial appropriate
for this? The array has only three values.

You used the word "iterate" to describe what you want to do. Does that
ring any bells? Right! Use Array#each or render :partial, :collection.

Also: consider using the array indices, not the actual words, for the
value attributes of the buttons.

BTW, why aren't you using Haml?

Also: the blank in the last radio button is obviously not in the
array. It's there to allow the user to not use this search
criterion. Is there a better way for a user to unselect a radio
button without choosing any other?

No. Use a <select> element instead (and check out collection_select).

Thanks.

Best,

Thanks Marnen. Using the array index and partial is a great idea.
The select and collection_select are also promising.

I'll take a look at Haml.

Thanks.

Hi Marnen,

The select and collection_select require an instance variable to be
passed to the view. But I am using form_tag and not form_for. So I
don't have an object to work with. This is a search form, the user
does not need to log in. So does it make sense to switch to
"form_for" for this search form in order to have an object to work
with?

Thanks.

Learn by Doing wrote:

Hi Marnen,

The select and collection_select require an instance variable to be
passed to the view. But I am using form_tag and not form_for. So I
don't have an object to work with.

So use select_tag.

This is a search form, the user
does not need to log in. So does it make sense to switch to
"form_for" for this search form in order to have an object to work
with?

Usually, yes. Do you have a Search class?

Thanks.

Best,

Hi Marnen,

Thank you so much for pointing me to using a Search class. I watched
the screencast on this http://media.railscasts.com/videos/111_advanced_search_form.mov
and realized this is what I should do so that the app can remember a
user's search.

My question now is How do I store the value of multiple checkboxes as
a result of using
"check_box" helper in a "Search" class? Please see my thread on this
question here: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/50915dfb233d62bc#

BTW, yes I should have looked at select_tag more closely.

Thanks!