List Box Issue - Should be easy, but nor for me!

I have the following snippet in my app:

<% @anitem = Item.find(:all, :conditions => "id = " + params[:id]) %>
<% form_for :anitem, :url => { :action => :updateitem } do |thisform|
-%>

… (some code…removed)

<%= collection_select :thisform, :item_category,
ItemCategory.find(:all), :id, :name %>

<%= collection_select :thisform, :packing_list_type,
PackingListType.find(:all), :id, :name %>
<%= submit_tag ‘Update’ %>
<% end %>

My issue is that the list box never selects the current value when it
pulls the data from the database.

Everything looks great - but no currently selected option tag
(“selected”).

Should be easy, but I can’t get it to work.

Any ideas?

On Dec 15, 1:37 pm, Ted S. [email protected] wrote:

<%= collection_select :thisform, :packing_list_type,
PackingListType.find(:all), :id, :name %>
<%= submit_tag ‘Update’ %>
<% end %>

When using the form_for stuff you need to do
thisform.collection_select :field_name,…

Fred

Well the documentation shows an example of how to select an element:

Do a view source and see if there is “selected” for an option. Put a
debug
statement and look at the log file to check if there is any value that
is
returned by the db.

When using the form_for stuff you need to do
thisform.collection_select :field_name,…

Thanks, Fred. Couldn’t get that to work. I tried various permutations.

I am using this, and it works, but it’s a hack of sorts:

<%= select(:aform, “packing_list_type_id”,
PackingListType.find(:all).collect {|p| [ p.name, p.id ] }, {:selected
=> @anitem[0][‘packing_list_type_id’]}) %>

It uses the :selected thingy to make it all work.

  • tedstur