How Do I Fix Collection_select?

The bug described here(http://dev.rubyonrails.org/ticket/1125) is biting
me hard at the moment, contrary to that ticket’s assertion that the bug
has been squashed.

For those without time to read the ticket, collection_select does not
support the :selected method, apparently because it uses
to_collection_select_tag instead of to_select_tag. Moreover, because of
this or some other deficiency, collection_select does not automatically
select the existing value when I move from my “new” view to my “edit”
view.

if self.can_fix_in_view_code?
question = “How?”
puts question
elsif !self.can_fix_in_view_code? and self.can_override_method?
question = “How do I override to_collection_select_tag?”
puts question
else
question = “What DO I do to fix collection_select?”
puts question
end

Thanks in advance,

-Matthew

collection_select does display the correct defaults when editing a
record. Double check your syntax, here is an example:

<%= form.collection_select :id, MyModel.find(:all), :id, :name %>

If the value of :id on the left matches one of :id options on the
right that will be the default value.

For the cases where you need to include a :selected parameter one easy
solution is to just use select instead of collection_select. It only
adds a couple of extra parameters and :selected option will work as
advertised. Another possibility would be to override the initialize
method in your model class to set the default value. Then when a new
object is passed into collection_select it will already have the
default and you don’t even need to use :selected.

Aaron

On Feb 12, 1:51 pm, Matthew F. [email protected]

Aaron wrote:

collection_select does display the correct defaults when editing a
record…

<%= form.collection_select :id, MyModel.find(:all), :id, :name %>

If the value of :id on the left matches one of :id options on the
right that will be the default value…

My code is essentially:

<% form_for :contact, @contact do |f| -%>
<%= f.collection_select :type_code, Lookup_contact_type.find(:all),
:description, :description, {:include_blank => true} %>
<% end -%>

type_code is a method in my Contact model, and Lookup_contact_type is
the model for its lookup table.

Is there a problem going cross-model like this? It works perfectly for
entering the data–just chokes on edit.

<% form_for :contact, @contact do |f| -%>
<%= f.collection_select :type_code, Lookup_contact_type.find(:all),
:description, :description, {:include_blank => true} %>
<% end -%>

So does the value of @contact.type_code for the record you are editing
match a description from one of the entries in
Lookup_contact_type.find(:all)? If so then that will be the default
selection. If you are not sure what the values are try adding some
debug calls to your view:

<%= debug @contact %>
<%= debug Lookup_contact_type.find(:all) %>

Aaron

So does the value of @contact.type_code for the record you are editing
match a description from one of the entries in
Lookup_contact_type.find(:all)?..

<%= debug @contact %>
<%= debug Lookup_contact_type.find(:all) %>

Thank you so very much for the debug code.

It turns out my schema was set to char on the field in question, which
was appending whitespace to the end of my lookup values as they were
entered into the contacts table.

Fixed all that and collection_select works like a charm.

Much obliged,

-Matthew