Ruby Forum Ruby on Rails > Populating Select Box with Hash Array

Posted by John Griffiths (indiehead)
on 28.11.2007 11:23
Hi, stuck with another little problem, select lists / drop down boxes.
Tried to avoid these because the code looks uber scary.

Anyway I've created a Hash Array called gender:

@gender = ['Man' => 'male', 'Woman' => 'female']

here you've got the key (being the english name) and the value (the
generic equivalent).

what I want to do is make a drop-down single-selection box use these
key/value properties to render it correctly,

<%= select("search", "type", @gender) %>

...this doesn't work but what i'd like is generate the form code...

<select name="search" id="type">
<option value="male">Man</option>
<option value="female">Woman</option>
</select>

I'd then read the value off the form via:

@selection = params[:search]

Where am I going wrong?, I know i need some kind of {key, value} clause
in the select() control but unsure where.

Any help really appreciated,

Sincerely


John.
Posted by Thorsten Mueller (thorsten)
on 28.11.2007 12:14
Posted by John Griffiths (indiehead)
on 28.11.2007 12:30
Thorsten Mueller wrote:
> maybe options_for_select would do the trick?
> http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000516

may well, thanks for that Thornsten ;-)

will try that tonight and see how it goes (god i love you people!)