My problem is that when I display an existing record (to edit it) with
the type_id field: the select displayed does not have the html
"selected" clause, so this combo always shows the first value in the
recovered list (@cm_board_types)... I've lot of similar cases working
perfectly, but this... I can not find where is the problem.
Instead of showing this:
<option value="1">Type Name for Value 1</option>
<option selected="selected" value="2">Type Name for Value 2</option>
is showing this:
<option value="1">Type Name for Value 1</option>
<option value="2">Type Name for Value 2</option>
About the form:
<p><%= f.select :type_id, (@cm_board_types.collect {|p| [p.name,
p.id]}), :required => true %></p>
Sorry, for the confusion
I meant that the object, you are passing to the form_for (in your case
it is @cm_board) should have the attribute "type_id" and the value
stored in that attribute should be equal to the index of the option
you want to have selected.
Here is the short note from the documentation:
"
select(object, method, choices, options = {}, html_options = {})
Create a select tag and a series of contained option tags for the
provided object and method. The option currently held by the object
will be selected, provided that the object is available. See
options_for_select for the required format of the choices parameter.
Example with @post.person_id => 1:
select("post", "person_id", Person.all.collect {|p| [ p.name,
p.id ] }, { :include_blank => true })
could become:
<select name="post[person_id]">
<option value=""></option>
<option value="1" selected="selected">David</option>
<option value="2">Sam</option>
<option value="3">Tobias</option>
</select>
This can be used to provide a default set of options in the standard
way: before rendering the create form, a new model instance is
assigned the default options and bound to @model_name. Usually this
model is not saved to the database. Instead, a second model object is
created when the create request is received. This allows the user to
submit a form page more than once with the expected results of
creating multiple records. In addition, this allows a single partial
to be used to generate form inputs for both edit and create forms.
By default, post.person_id is the selected option. Specify :selected
=> value to use a different selection or :selected => nil to leave all
options unselected. Similarly, you can specify values to be disabled
in the option tags by specifying the :disabled option. This can either
be a single value or an array of values to be disabled."