collection_select :selected (to get a default value into <se

1,356 views
Skip to first unread message

Steve Davie

unread,
Feb 6, 2009, 11:35:32 AM2/6/09
to rubyonra...@googlegroups.com
I'm getting murdered by this thing! For the life of me, I can't figure
it out. I've spent literally days on google.

Here's what I would like to do (in rails 2.2.2):


<% if params[:cruise_info] %>
<%= collection_select :cabin_type, :cabin_type_id,
CabinType.find_by_status(1).collect {|ct| ct}, :id, :name, {:selected =>
params[:cruise_info][:cabin_type_id]}%>
<% end %>

The intent here is, on re-rendering the page (after a validation
failure), I don't want the end-user to be required to make the selection
again. I have half a dozen collection_selects in this form that are
being returned from partials and are embeded with :fields_for, so
they're not automatically retaining their selections.

Most of my search results are Bug reports for this, but they're more
than 2 years old. It's gotta be fixed by now right? I have to be doing
something wrong? I just can't see it. I've tried using select,
options_from_collection_for_select, and have the same issue with all of
them. I've tried different variations, like putting it in the options
=> {} and in html_options => {}...and nothing. I've tried :selected,
:selected_value, I even tried using :include_blank => {...}, which i
knew wouldn't work, but thought it worth a try. I've tried to_s on the
params (which btw are an integer), but i'm not even seeing the
"selected" option showing up in the html source.

Sorry for being so long winded, I want to insure I include enough
information to get this solved.

Any advice?

Thanks in advance
--
Posted via http://www.ruby-forum.com/.

Frederick Cheung

unread,
Feb 6, 2009, 11:47:23 AM2/6/09
to rubyonra...@googlegroups.com

On 6 Feb 2009, at 16:35, Steve Davie wrote:

>
> I'm getting murdered by this thing! For the life of me, I can't
> figure
> it out. I've spent literally days on google.
>
> Here's what I would like to do (in rails 2.2.2):
>
>
> <% if params[:cruise_info] %>
> <%= collection_select :cabin_type, :cabin_type_id,
> CabinType.find_by_status(1).collect {|ct| ct}, :id, :name,
> {:selected =>
> params[:cruise_info][:cabin_type_id]}%>
> <% end %>

collection_select ignore the :selected option.

Fred

Frederick Cheung

unread,
Feb 6, 2009, 11:48:47 AM2/6/09
to Frederick Cheung, rubyonra...@googlegroups.com

On 6 Feb 2009, at 16:47, Frederick Cheung wrote:

>
> On 6 Feb 2009, at 16:35, Steve Davie wrote:
>
>>
>> I'm getting murdered by this thing! For the life of me, I can't
>> figure
>> it out. I've spent literally days on google.
>>
>> Here's what I would like to do (in rails 2.2.2):
>>
>>
>> <% if params[:cruise_info] %>
>> <%= collection_select :cabin_type, :cabin_type_id,
>> CabinType.find_by_status(1).collect {|ct| ct}, :id, :name,
>> {:selected =>
>> params[:cruise_info][:cabin_type_id]}%>
>> <% end %>
>
> collection_select ignore the :selected option.
>
should add that this will change in 2.3

Fred

Steve Davie

unread,
Feb 6, 2009, 11:55:04 AM2/6/09
to rubyonra...@googlegroups.com
Frederick Cheung wrote:

> On 6 Feb 2009, at 16:47, Frederick Cheung wrote:
>
>>>
>>> <% if params[:cruise_info] %>
>>> <%= collection_select :cabin_type, :cabin_type_id,
>>> CabinType.find_by_status(1).collect {|ct| ct}, :id, :name,
>>> {:selected =>
>>> params[:cruise_info][:cabin_type_id]}%>
>>> <% end %>
>>
>> collection_select ignore the :selected option.
>>
> should add that this will change in 2.3
>
> Fred

Thanks for the info Fred, but do you know of any convention for handling
this kind of problem now?

Frederick Cheung

unread,
Feb 6, 2009, 12:50:40 PM2/6/09
to rubyonra...@googlegroups.com

On 6 Feb 2009, at 16:55, Steve Davie wrote:

>
> Frederick Cheung wrote:
>> On 6 Feb 2009, at 16:47, Frederick Cheung wrote:
>>
>>>>
>>>> <% if params[:cruise_info] %>
>>>> <%= collection_select :cabin_type, :cabin_type_id,
>>>> CabinType.find_by_status(1).collect {|ct| ct}, :id, :name,
>>>> {:selected =>
>>>> params[:cruise_info][:cabin_type_id]}%>
>>>> <% end %>
>>>
>>> collection_select ignore the :selected option.
>>>
>> should add that this will change in 2.3
>
> Thanks for the info Fred, but do you know of any convention for
> handling
> this kind of problem now?

You could just use a regular select. Or if the cabin_type_id attribute
of the corresponding model object is set to params[:cruise_info]
[:cabin_type_id] then it will just work.

Fred

Steve Davie

unread,
Feb 6, 2009, 1:40:46 PM2/6/09
to rubyonra...@googlegroups.com
Frederick Cheung wrote:

> On 6 Feb 2009, at 16:55, Steve Davie wrote:
>
>>>>> <% end %>
>>>>
>>>> collection_select ignore the :selected option.
>>>>
>>> should add that this will change in 2.3
>>
>> Thanks for the info Fred, but do you know of any convention for
>> handling
>> this kind of problem now?
>
> You could just use a regular select. Or if the cabin_type_id attribute
> of the corresponding model object is set to params[:cruise_info]
> [:cabin_type_id] then it will just work.
>
> Fred

Thanks again Fred, I think I'll have to just write out the selects. I
was hoping that maybe this was more common and there would be a "rails"
solution for it. The engineering of my forms must be out of whack with
convention. I'll swing over to railscasts for a re-review of complex
forms.

Julian Leviston

unread,
Feb 6, 2009, 10:51:52 PM2/6/09
to rubyonra...@googlegroups.com
I'm a little unsure as to what you want to do? Is it that you want a
default value and then to retain their selected value? Or something
else. I remember understanding collection select took me a while. It
helps if you can do the HTML long hand once through to get clear about
exactly what behaviour you want at first.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 07/02/2009, at 3:55 AM, Steve Davie <rails-mailing-list@andreas-

Miguel Regedor

unread,
Feb 7, 2009, 7:28:17 AM2/7/09
to Ruby on Rails: Talk
if you have a form you can do:

f.collection_select :cabin_type_id, CabinType.find_by_status
(1).collect {|ct| ct}, :id, :name, :prompt => " -- Select -- "

and it should mantain the selected values.

Other way (because collection_select ignore the :selected option) I
would sugest you to use a select_tag in conjunction with options_for
select.

Bharat

unread,
Feb 8, 2009, 9:36:26 AM2/8/09
to Ruby on Rails: Talk
I suggest that you buy and watch Forms screencasts by Ryan Bates
published by Pragmatic Programmers. He explains proper use of
collection_select. I have learned a lot by watching the screencasts.

http://www.pragprog.com/screencasts/v-rbforms/mastering-rails-forms

As per answering your question, I am also not sure of what is it that
you are trying to do? If you are simply trying to set initial/
selected default value when creating/editing a record, then you should
be doing that in the controller action and not in the view. Ryan
explains it very clearly in his screencasts.
Bharat
Reply all
Reply to author
Forward
0 new messages