Simple dropdown menu

Hi, I’m quite new to this language and I’m having trouble finding out
simple things like a dropdown menu in rhtml. I know I have to use <%
select_tag %> but I’ve read lots of different ways to put in the options
so I’m a bit confused. Options are hard-coded for the menu I’m trying to
create so no database is involved!

Thanks v much!
Bex

Hi Becky –

On 13-Jul-06, at 4:14 AM, Becky F. wrote:

Hi, I’m quite new to this language and I’m having trouble finding out
simple things like a dropdown menu in rhtml. I know I have to use <%
select_tag %> but I’ve read lots of different ways to put in the
options
so I’m a bit confused. Options are hard-coded for the menu I’m
trying to
create so no database is involved!

You can do it one of two ways: supply a string of option tags as the
second argument to select_tag, or use the options_for_select helper
to generate the option tags for you. Here’s an example of the former.

select_tag ‘categories’, ‘RubyRails’

That’s pretty ghetto, though, what with all those option tags; you
should use the options_for_select helper instead, passing in your
options as an array:

select_tag ‘categories’, options_for_select([‘Ruby’, ‘Rails’])

Or, assuming you want ‘values’ for each of your options, pass in Hash
who’s keys are the names of the options and who’s values are the
value attributes:

select_tag ‘categories’, options_for_select({‘Ruby’ => ‘1’, ‘Rails’
=> ‘2’})

If you want to pre-select a certain option, the second argument to
options_for_select is the cheese:

select_tag ‘categories’, options_for_select({‘Ruby’ => ‘1’, ‘Rails’
=> ‘2’}, ‘1’)

The final example would output something like:

Ruby Rails

HTH

/Jeff


http://re.visioni.st
http://quotedprintable.com

I’m not sure about v1.2, but from the below snippet, ‘Ruby’ is
pre-selected. If i choose ‘Rails’, and refresh my browser, ‘Rails’ would
still be chosen instead of ‘Ruby’.

Ideally, I’d like ‘Ruby’ to be selected again if the page refreshes…
ideas anyone?

D

Jeffrey H. wrote:

Hi Becky –

If you want to pre-select a certain option, the second argument to
options_for_select is the cheese:

select_tag ‘categories’, options_for_select({‘Ruby’ => ‘1’, ‘Rails’
=> ‘2’}, ‘1’)

The final example would output something like:

Ruby Rails

HTH

/Jeff


http://re.visioni.st
http://quotedprintable.com

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