user483040
user483040

Reputation:

Selection dropdown in Ruby on Rails: selected value not being picked up

I've got this problem where I get the dropdown showing but the selected option is not showing up. Here's the code I have to generate the selection dropdown in the .erb:

<%= collection_select("url", "source_type_id", @source_types, :id, :name, {:prompt => "Please select..."}) %>

The @source_types is populated in the controller from a lookup table that is tied to the model. @url_object is the model:

@source_types = SourceType.all

Because of the way the model is tied to the lookup table:

belongs_to :source_type

@url_object.source_type_id returns the numeric value, and @url_object.source_type returns the associated name from the lookup table.

<select id="url_source_type_id" name="url[source_type_id]"><option value="">Please   select...</option>
<option value="1">Dictionary/Thesaurus</option>
<option value="2">Encyclopedia</option>
<option value="3">Magazine</option>
<option value="4">Map/Atlas</option>
<option value="5">Newspaper</option>
<option value="6">Reference Tools</option></select>

I read the API for this method and the implication is that if the source_type_id is present the collection_select will automagically pick it up and set the selected value, but this is clearly not happening.

I'm hoping someone will see what obvious mistake I've made here...

Upvotes: 0

Views: 2888

Answers (1)

supriya
supriya

Reputation: 108

Hope this will help <%= f.select :source_type_id, @source_types.collect{|p| [p.name, p.id] }, params[:source_type_id] %>

Upvotes: 1

Related Questions