Maintaining drop down menu selections

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.

still not close?

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

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.

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

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000944

options_from_collection_for_select may be another alternative and the
“choices” parameter of form.select works just the same as
options_for_select

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