tedster
December 15, 2007, 2:37pm
1
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?
tedster
December 15, 2007, 5:16pm
2
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
tedster
December 16, 2007, 2:04am
3
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.
tedster
December 16, 2007, 4:43am
4
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.