# Maintaining drop down menu selections

**URL:** <https://www.ruby-forum.com/t/maintaining-drop-down-menu-selections/135634>\
**Category:** Rails\
**Created:** [April 23, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/maintaining-drop-down-menu-selections/135634 "2008-04-23T20:13:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rushnosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rushnosh](https://www.ruby-forum.com/u/rushnosh)\
**Post date:** [April 23, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/maintaining-drop-down-menu-selections/135634/1 "2008-04-23T20:13:16Z")

</div>

i am creating multiple drop down menus using

\<%= form.select(:choice, @list.map { | item | [item[0], item[1] ] })  
%\>

this works great but when the client hits submit and there are other  
validation errors  
it resets the drop down menu.

how do i maintain the selected item.

my search for the options of the slect method has come up empty.

@list is created in the controller and used in the view.  
can you not test :choice to see if the user has choose an item and  
then display the drop down with the correct item selected.

thanks.

---

<div class="post-metadata">

**Author:** ![rushnosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rushnosh](https://www.ruby-forum.com/u/rushnosh)\
**Post date:** [April 23, 2008, 9:31pm UTC](https://www.ruby-forum.com/t/maintaining-drop-down-menu-selections/135634/2 "2008-04-23T21:31:08Z")

</div>

still not close?

On Apr 23, 11:30 am, Thorsten M. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![rushnosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rushnosh](https://www.ruby-forum.com/u/rushnosh)\
**Post date:** [April 23, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/maintaining-drop-down-menu-selections/135634/3 "2008-04-23T21:44:49Z")

</div>

1   
2 \<% if @stuff.item\_id == 0 or @stuff.item\_id == nil -%\>  
3 Please Selected An Item\</  
option\>  
4 \<% end -%\>  
5 \<% @items = Item.find(:all, :order =\> “name”)  
6 @items.each do |item| %\>  
7 \<option value="\<%= item.id %\>"  
8 \<% if @stuff.item\_id == item.id -%\> selected=“selected”\<% end -%\>  
9 \>  
10 \<%= item.name %\> (\<%= item.description %\>)  
11   
12 \<% end %\>  
13

is this the only method to handle a drop down menu.  
looks like it is comparing the value selected with the item and then  
setting to selected.  
this would be a pain if you have a lot of drop down menus in an  
application.

any suggestions to preserving the item selected by a user. the items  
of the drop down menu do not come from a simple search, at times they  
are combined from multiple searches, but basically i have an array,  
with display item, id# which is passed to the view.

---

<div class="post-metadata">

**Author:** ![rushnosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58956e/32.png) [@rushnosh](https://www.ruby-forum.com/u/rushnosh)\
**Post date:** [April 23, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/maintaining-drop-down-menu-selections/135634/4 "2008-04-23T20:30:35Z")

</div>

sorry, don’t have the time to go into details right now, but maybe  
that’ll help you a bit:

\<%= select\_tag “client[branch][]”,  
options\_for\_select([[‘Automatisering’,‘1’],  
[‘Bouwnijverheid’, ‘2’],  
[‘Communicatie Intern’, ‘3’], [‘Communicatie  
Extern’, ‘4’], [‘Cultuur’, ‘5’],  
[‘Detailhandel food’, ‘6’], [‘Detailhandel non  
food’, ‘7’], [‘Drukkerij’, ‘8’],  
[‘Facilitaire dienst’, ‘9’], [‘Financiele  
dienstverlener’, ‘10’],  
[‘Gezondheidszorg’, ‘11’],  
[‘Handel’, ‘12’],  
[‘Industrie’, ‘13’], [‘IT’, ‘14’],  
[‘Mediabureau’, ‘15’],  
[‘Overheid’, ‘16’], [‘Onderwijs’, ‘17’],  
[‘Onderzoek’, ‘18’],  
[‘Reclamebureau’, ‘19’], [‘Recreatie &  
toerisme’, ‘20’],  
[‘Telecom’, ‘21’], [‘Transport sector’, ‘22’],  
[‘Uitgeverij dag-en weekbladen’, ‘23’],  
[‘Uitgeverij publieksbladen’, ‘24’], [‘Uitgeverij vakbladen’, ‘25’],  
[‘Uitgeverij relatiemagazines’, ‘26’],  
[‘Zakelijk adviesbureau’, ‘27’], [‘Overig’,  
‘28’]], @client.branch), :multiple =\> true, :size =\> 7, :id =\>  
“client\_branch” %\>

in this case @client.branch holds an array of ids that are selected, but  
could be a single element too

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

options\_from\_collection\_for\_select may be another alternative and the  
“choices” parameter of form.select works just the same as  
options\_for\_select
