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

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

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

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

Frederick Cheung wrote:

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/

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