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

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

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

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

On 6 Feb 2009, at 16:47, Frederick C. 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

Frederick C. wrote:

On 6 Feb 2009, at 16:47, Frederick C. 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?

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

Frederick C. 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.

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-

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.

I suggest that you buy and watch Forms screencasts by Ryan B.
published by Pragmatic Programmers. He explains proper use of
collection_select. I have learned a lot by watching the screencasts.

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