Forum: Ruby on Rails collection_select question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1b6ed15a6126e1d5ef4d7d15550f30bb?d=identicon&s=25 François Montel (zerohalo)
on 2006-06-10 10: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 =>
@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.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-06-10 14: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| [producer.name, producer.id] }
        select 'YOUR_MODEL', 'producer_id', options %>

Craig
1b6ed15a6126e1d5ef4d7d15550f30bb?d=identicon&s=25 François Montel (zerohalo)
on 2006-06-10 18:24
Craig White 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:

<select id="selectionfilter_producer_id"
name="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
around).

<select id="selectionfilter_producer_id"
name="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!
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-01-11 21:49
Francois, did you figure out a way to do this?

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

Thanks,
Wes
This topic is locked and can not be replied to.