Select_tag without selected="selected" clause

Hi,

in my controller, for an edit action, I’m getting the whole list of
values from a table with types:

@cm_board_types = CmBoardType.find(:all, :conditions => [‘project_id=?’,
@project.id])

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:

Type Name for Value 1 Type Name for Value 2

is showing this:

Type Name for Value 1 Type Name for Value 2

About the form:

<%= f.select :type_id, (@cm_board_types.collect {|p| [p.name, p.id]}), :required => true %>

Thanks in advance

Please, make sure that the object in your form_for helper:

  1. object.response_to? “type_id”
  2. object.type_id == 2

    Thanks, Ivan Povalyukhin

Thanks for your answer. But, I don’t know exactly what you mean.

First is renderized the edit.rhtml form. From this one:

<%= render :partial => ‘edit’ %>

The partial _edit.rhtml form:
<% labelled_tabular_form_for :cm_board, @cm_board,
:url => {:action => ‘edit’, :id => @cm_board},
:html => {:id => ‘edit-cm_board-form’,
:class => nil,
:multipart => true} do |f| %>

and finally the _attributes.rhtml form:
<% fields_for :cm_board, @cm_board, :builder => TabularFormBuilder do
|f| %>

The labelled_tabular_form_for helper looks like:
def labelled_tabular_form_for(name, object, options, &proc)
options[:html] ||= {}
options[:html][:class] = ‘tabular’ unless
options[:html].has_key?(:class)
form_for(name, object, options.merge({ :builder => TabularFormBuilder,
:lang => current_language}), &proc)
end

where can I search for the info you request?

Sorry, for the confusion :slight_smile:
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:


David
Sam
Tobias

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."

Thanks, Ivan Povalyukhin