# Struggling with formhelper select tag

**URL:** <https://www.ruby-forum.com/t/struggling-with-formhelper-select-tag/130285>\
**Category:** Rails\
**Created:** [February 27, 2008, 2:59am UTC](https://www.ruby-forum.com/t/struggling-with-formhelper-select-tag/130285 "2008-02-27T02:59:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [February 27, 2008, 2:59am UTC](https://www.ruby-forum.com/t/struggling-with-formhelper-select-tag/130285/1 "2008-02-27T02:59:16Z")

</div>

Hi -

There are two basic cases where I want to use FormHelper (form\_for) to  
create select tags:

1. Where the options come from another table
2. Where the options come from a manually created list or hash

An example of 1 might be a tagging application. I may have a photo  
sharing site that lets users select a tag from a list box (select) and  
the tags come from a table.

An example of 2 might be a new/edit user form where gender is male or  
female and male is stored as 1 and female is stored in 2.

I cannot find a simple example of these 2 cases. So,

a) can someone post a simple example of both  
b) where does a new rails developer find docs for this stuff?

Thanks,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [February 27, 2008, 10:25am UTC](https://www.ruby-forum.com/t/struggling-with-formhelper-select-tag/130285/2 "2008-02-27T10:25:29Z")

</div>

ok, this one does both at once:

\<% form\_tag(invitations\_path(), :method =\> :get) do %\>  
\<%= label(:registration\_group, ‘Group’) %\>  
\<%= select(:registration\_group, :id, [[“all”, 0]] +  
(@registration\_groups.collect{|rg| [rg.name, rg.id]}),  
{:selected =\> 0)}, {:onchange =\> “this.form.submit();”})  
%\>  
\<% end %\>

the idea is to select a group for invitations to be shown  
together with an “all” entry

the basic thing is to hand over an array with the key/value pairs  
you can use a simple array as the all entry [[“all”, 0], [“more”, 1]]  
or collect an db-record into such an array  
and as here, combine several arrays as long as the ids don’t conflict

selected is always zero here, you could give it any key  
onchange triggers the form submit, you could use a ‘normal’ submit  
instead

you’ll find this documented here:  
[http://api.rubyonrails.org/](http://api.rubyonrails.org/)

the usage of select:

> **[ActionView::Helpers::FormOptionsHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000940)**
>
> Provides a number of methods for turning different kinds of containers into a set of option tags.

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [February 27, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/struggling-with-formhelper-select-tag/130285/3 "2008-02-27T12:01:43Z")

</div>

> you’ll find this documented here:[http://api.rubyonrails.org/](http://api.rubyonrails.org/)
> 
> ## the usage of select:[http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelp](http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelp)…
> 
> Posted viahttp://www.ruby-forum.com/.

Thanks so much. This was a vocabulary problem. I was looking under  
FormHelper and saw no select tag. I didn’t realize there was a  
FormOptionsHelper.

Thanks again,  
Dino
