Forum: Ruby on Rails collection_select question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
François M. (Guest)
on 2006-06-10 12:10
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 =>

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 =>

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
Craig W. (Guest)
on 2006-06-10 16:55
(Received via mailing list)
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| [,] }
        select 'YOUR_MODEL', 'producer_id', options %>

François M. (Guest)
on 2006-06-10 20:24
Craig W. wrote:
> (controller code)
>   @producer = Producer.find(:all)
> (view code)
>  <%= options = [['Select', '']] + @producer.collect {
>         |producer| [,] }
>         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:

<select id="selectionfilter_producer_id"
<option value=""></option>
<option value="1">Bob</option>
<option value="2">Harry</option>
<option value="3">John</option>

when I want it to yield this (assuming the user chose "Harry" last time

<select id="selectionfilter_producer_id"
<option value=""></option>
<option value="1">Bob</option>
<option selected value="2">Harry</option>
<option value="3">John</option>

Any other clues? Thanks!
Wes G. (Guest)
on 2007-01-11 22:49
Francois, did you figure out a way to do this?

Do you know how to set multiple options to be selected?

This topic is locked and can not be replied to.