How do I specify which option should be initially selected when using
the collection_select command? I’ve tried:
collection_select(:selectionfilter, :producer_id, Producer.find(:all),
:id, :name, {:include_blank => true, :selected_value =>
@initial_selection})
However it doesn’t return the desired results (the option with the value
of @initial_selection selected).
I’ve also tried:
collection_select(:selectionfilter, :producer_id, Producer.find(:all),
:id, :name, {:include_blank => true}, {:selected_value =>
@initial_selection})
which likewise doesn’t work. I’ve verified that @initial_selection is
returning the correct value.
The docs say that collection_select uses the
options_for_collection_select, which has :selected_value as one of its
options, so it seems the above should work, but I’m obviously missing
something.
On Sat, 2006-06-10 at 10:10 +0200, François Montel wrote:
I’ve also tried:
options, so it seems the above should work, but I’m obviously missing
something.
that’s not how I would use it. Assuming that ‘your model’ is related to
producers, I would use it something more like this…
(controller code)
@producer = Producer.find(:all)
(view code)
<%= options = [[‘Select’, ‘’]] + @producer.collect {
|producer| [producer.name, producer.id] }
select ‘YOUR_MODEL’, ‘producer_id’, options %>
Craig
Francois, did you figure out a way to do this?
Do you know how to set multiple options to be selected?
Thanks,
Wes
Craig W. wrote:
(controller code)
@producer = Producer.find(:all)
(view code)
<%= options = [[‘Select’, ‘’]] + @producer.collect {
|producer| [producer.name, producer.id] }
select ‘YOUR_MODEL’, ‘producer_id’, options %>
Thanks, Craig, but your code above yields the same results that I was
getting. (It seems to me, my original code is a shortcut for the above.)
The problem I’m having is not with the select box malfunctioning, but
rather that the select box is not displaying the initial value that I
want it to. It’s used to set a filter, and when the user selects a
value, the page is refreshed, filtered on that value. At that point I
would like the select box to display the last value selected (the value
that is being filtered on), which I have stored in @initial_selection.
However, despite using the “:selected” option, the select box still show
the default first value. That’s what I’m trying to solve.
In other words, my code is yielding this:
Bob
Harry
John
when I want it to yield this (assuming the user chose “Harry” last time
around).
Bob
Harry
John
Any other clues? Thanks!