# Simple dropdown menu

**URL:** <https://www.ruby-forum.com/t/simple-dropdown-menu/98229>\
**Category:** Rails\
**Created:** [July 13, 2006, 10:14am UTC](https://www.ruby-forum.com/t/simple-dropdown-menu/98229 "2006-07-13T10:14:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [July 13, 2006, 10:14am UTC](https://www.ruby-forum.com/t/simple-dropdown-menu/98229/1 "2006-07-13T10:14:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [July 13, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/simple-dropdown-menu/98229/2 "2006-07-13T16:47:53Z")

</div>

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://re.visioni.st)  
[http://quotedprintable.com](http://quotedprintable.com)

---

<div class="post-metadata">

**Author:** ![bex3000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@bex3000](https://www.ruby-forum.com/u/bex3000)\
**Post date:** [March 9, 2007, 9:41am UTC](https://www.ruby-forum.com/t/simple-dropdown-menu/98229/3 "2007-03-09T09:41:11Z")

</div>

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://re.visioni.st)  
> [http://quotedprintable.com](http://quotedprintable.com)
