Forum: Ruby on Rails collection_select default selected value

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.
5e518814b76962fdd1ad0e74dfac5ea7?d=identicon&s=25 Martin Gamsjaeger (Guest)
on 2006-05-22 19:56
(Received via mailing list)
Hiall,

Unfortunately I just can't find out how to setup a default selected
value when using collection_select. My call is like so:

<%= collection_select(:consultant, :lastname, @consultants, :id,
:lastname, { :selected => @current_consultant.id } )  %>

which is not working, I debugged so far that I know that
@current_consultant.id contains the correct value.

Any tips?

cheers
Martin
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-05-22 20:25
(Received via mailing list)
On Monday, May 22, 2006, at 7:56 PM, Martin Gamsjaeger wrote:
>
>Any tips?
>
>cheers
>Martin
>
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails
>
What does the generated HTML look like?

_Kevin
5e518814b76962fdd1ad0e74dfac5ea7?d=identicon&s=25 Martin Gamsjaeger (Guest)
on 2006-05-22 21:37
(Received via mailing list)
Hi Kevin,

Sorry for replying late! The generated HTML looks like:

<select id="communal_audit_consultant_lastname"
name="communal_audit_consultant[lastname]"><option
value="1">Auinger</option>
<option value="2">Katterl</option></select>



On 22 May 2006 18:22:50 -0000, Kevin Olbrich
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-05-22 22:02
(Received via mailing list)
On Monday, May 22, 2006, at 9:35 PM, Martin Gamsjaeger wrote:
>
>> >:lastname, { :selected => @current_consultant.id } )  %>
>> >Rails mailing list
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails
>

try this...

:selected => @current_consultant.id.to_s

The 'values' that the select compares against are really strings.  Not
sure if they get properly cast for the purposes of the comparison needed
to set the selected one.

_Kevin
5e518814b76962fdd1ad0e74dfac5ea7?d=identicon&s=25 Martin Gamsjaeger (Guest)
on 2006-05-22 22:18
(Received via mailing list)
Thx for the tip but that didn't work. In the meantime I found out that a
call to
<% options = options_from_collection_for_select(@consultants, :id,
:lastname, @current_consultant.id) %>

produces correct option tags like:

<option value="1">Auinger</option>
<option value="2" selected="selected">Katterl</option>

However, I find no way to wrap this into a select tag. ..

There must be an easier way???

cheers
Martin

On 22 May 2006 20:01:42 -0000, Kevin Olbrich
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-05-22 22:34
(Received via mailing list)
On Monday, May 22, 2006, at 10:17 PM, Martin Gamsjaeger wrote:
>However, I find no way to wrap this into a select tag. ..
>> >Hi Kevin,
>> >On 22 May 2006 18:22:50 -0000, Kevin Olbrich
>> >> >
>> >> >Rails@lists.rubyonrails.org
>> >> Rails@lists.rubyonrails.org
>>
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails
>

Try
<%= select_tag 'communal_audit_consultant_lastname', options %>

_Kevin
5e518814b76962fdd1ad0e74dfac5ea7?d=identicon&s=25 Martin Gamsjaeger (Guest)
on 2006-05-22 22:41
(Received via mailing list)
I've tried that before :) This leads to very strange:

<select id="communal_audit_consultant_lastname"
name="communal_audit_consultant[lastname]"><option value="&lt;option
value=&quot;1&quot;&gt;Auinger&lt;/option&gt;
">&lt;option value=&quot;1&quot;&gt;Auinger&lt;/option&gt;
</option>
<option value="&lt;option value=&quot;2&quot;
selected=&quot;selected&quot;&gt;Katterl&lt;/option&gt;">&lt;option
value=&quot;2&quot;
selected=&quot;selected&quot;&gt;Katterl&lt;/option&gt;</option></select>

showing the whole option strings in the dropdown ...


On 22 May 2006 20:34:13 -0000, Kevin Olbrich
5e518814b76962fdd1ad0e74dfac5ea7?d=identicon&s=25 Martin Gamsjaeger (Guest)
on 2006-05-22 23:02
(Received via mailing list)
I also tried to give the parameters as a hash with keys corresponding
to parameters of options_from_collection_for_select. This also doesn't
lead to anything ...
5e518814b76962fdd1ad0e74dfac5ea7?d=identicon&s=25 Martin Gamsjaeger (Guest)
on 2006-05-22 23:19
(Received via mailing list)
Going through the AWDWR book I decided to go another way. I now have

@consultants = CommunalAuditConsultant.find(:all, :order => "lastname"
).map {|c| [c.lastname, c.id] }

in my controller, and then

<%= select(:communal_audit_consultant, :id, @consultants)  %>

in my partial

This works perfectly!

Thx for your patience and tips, Kevin!

cheers
Martin
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-05-23 03:32
(Received via mailing list)
You could add in your controller method...

@consultants.lastname = 'bla'

Craig
Fa06489a97b0efe3c3b7d249982878d3?d=identicon&s=25 divotdave (Guest)
on 2007-03-01 21:47
Hey All:

Martin Gamsjaeger wrote:
> I've tried that before :) This leads to very strange:
>
> <select id="communal_audit_consultant_lastname"
> name="communal_audit_consultant[lastname]"><option value="&lt;option
> value=&quot;1&quot;&gt;Auinger&lt;/option&gt;
> ">&lt;option value=&quot;1&quot;&gt;Auinger&lt;/option&gt;
> </option>
> <option value="&lt;option value=&quot;2&quot;
> selected=&quot;selected&quot;&gt;Katterl&lt;/option&gt;">&lt;option
> value=&quot;2&quot;
> selected=&quot;selected&quot;&gt;Katterl&lt;/option&gt;</option></select>
>
> showing the whole option strings in the dropdown ...
>
>
> On 22 May 2006 20:34:13 -0000, Kevin Olbrich

This has always worked for me - just follow the docs ;)

<%= select_tag 'my[select]',
options_from_collection_for_select(Model.find(:all),'id','name',some_string)
%>

where some_string = an actual string (i.e. 'Joe') or a variable (ala
@person.name).

Works like a charm - everytime.

Cheers,
divotdave
26b698a77a5e28dd47501cfd1d9732c7?d=identicon&s=25 Ricla Semon (ricla)
on 2009-05-02 02:37
Martin is right, but would like to add a comment. If you do

<%= select_tag 'my[select]',
options_from_collection_for_select(Model.find(:all),'id','name',some_string)
%>

Then you do need to pass some_string as string. However, if you are
passing  symbols, the you would need to convert to the symbol's type:

<%= select_tag 'my[select]',
options_from_collection_for_select(Model.find(:all),:id,:name,some_string.to_i)
%>

Hope that helps someone

Riclaso

divotdave wrote:
> Hey All:
>
> Martin Gamsjaeger wrote:
>> I've tried that before :) This leads to very strange:
>>
>> <select id="communal_audit_consultant_lastname"
>> name="communal_audit_consultant[lastname]"><option value="&lt;option
>> value=&quot;1&quot;&gt;Auinger&lt;/option&gt;
>> ">&lt;option value=&quot;1&quot;&gt;Auinger&lt;/option&gt;
>> </option>
>> <option value="&lt;option value=&quot;2&quot;
>> selected=&quot;selected&quot;&gt;Katterl&lt;/option&gt;">&lt;option
>> value=&quot;2&quot;
>> selected=&quot;selected&quot;&gt;Katterl&lt;/option&gt;</option></select>
>>
>> showing the whole option strings in the dropdown ...
>>
>>
>> On 22 May 2006 20:34:13 -0000, Kevin Olbrich
>
> This has always worked for me - just follow the docs ;)
>
> <%= select_tag 'my[select]',
> options_from_collection_for_select(Model.find(:all),'id','name',some_string)
> %>
>
> where some_string = an actual string (i.e. 'Joe') or a variable (ala
> @person.name).
>
> Works like a charm - everytime.
>
> Cheers,
> divotdave
8ccd7540eb289898325223bc661abf6b?d=identicon&s=25 Lisa Lee (learnruby)
on 2009-09-24 09:13
Hi,

I have some issues with setting the selected value. On my page, user
selects a State from the drop down and other attributes and click on
"Find". I want to set the default value of the drop down to the value
selected prior to clicking "Find".

<%=collection_select(:state, :state_id, @states, :state_id, :state_name,
          {:prompt => true,  :selected => @selected_state},
          {:style => 'width:200px;',
               :onchange => remote_function(:url =>
                 {:action => 'update_cities'},:with =>
"'state_id='+value")})%>

In my controller, if I have @selected_state = 1, the dropdown default
value is shown in the UI. However,  if I have @selected_state =
state_id, the default value is not set.

Snippet of the code in my controller:

def find
    state_id = params[:state][:state_id]

    @selected_state = state_id

    /* Some logic here */

    @states = State.find(:all)

   respond_to do |format|
        format.html { render :action => "index" }
   end
 end

Any pointers are appreciated. Thanks.
40de15fd53f2786cbc31ad2adb9a2b80?d=identicon&s=25 Zt Di (horliao)
on 2009-09-26 12:42
(Received via mailing list)
This is useful about collection_select :)

http://shiningthrough.co.uk/blog/show/6

On Sep 24, 12:13 am, Eunice Tan <rails-mailing-l...@andreas-s.net>
8ccd7540eb289898325223bc661abf6b?d=identicon&s=25 Lisa Lee (learnruby)
on 2009-10-01 09:29
Thanks for the link Zt Di.

@selected_state = state_id.to_i works.

This is because state_id = params[:state][:state_id] returns a string.
F80c5168b92fe9d49b0668cde3bdd57c?d=identicon&s=25 ct9a (Guest)
on 2009-10-20 14:24
(Received via mailing list)
Agree with you . Someone should merge the documentation there to the
Rails API. The rails api's collection_select doc is somewhat
incomplete or too brief. In short, it's not helpful.
This topic is locked and can not be replied to.