# select\_tag without selected="selected" clause

**URL:** <https://discuss.rubyonrails.org/t/select-tag-without-selected-selected-clause/56387>\
**Category:** rubyonrails-talk\
**Created:** [December 22, 2010, 7:37am UTC](https://discuss.rubyonrails.org/t/select-tag-without-selected-selected-clause/56387 "2010-12-22T07:37:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 22, 2010, 7:37am UTC](https://discuss.rubyonrails.org/t/select-tag-without-selected-selected-clause/56387/1 "2010-12-22T07:37:13Z")

</div>

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: \<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\>

Thanks in advance

---

<div class="post-metadata">

**Author:** ![ivanpoval](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@ivanpoval](https://discuss.rubyonrails.org/u/ivanpoval)\
**Post date:** [December 22, 2010, 8:46am UTC](https://discuss.rubyonrails.org/t/select-tag-without-selected-selected-clause/56387/2 "2010-12-22T08:46:47Z")

</div>

Please, make sure that the object in your form\_for helper: 1. object.response\_to? "type\_id" 2. object.type\_id == 2

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 22, 2010, 3:53pm UTC](https://discuss.rubyonrails.org/t/select-tag-without-selected-selected-clause/56387/3 "2010-12-22T15:53:26Z")

</div>

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, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:url =\> {:action =\> 'edit', :id =\> @cm\_board}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:html =\> {:id =\> 'edit-cm\_board-form', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:class =\> nil, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: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) &nbsp;&nbsp;options[:html] ||= {} &nbsp;&nbsp;options[:html][:class] = 'tabular' unless options[:html].has\_key?(:class) &nbsp;&nbsp;form\_for(name, object, options.merge({ :builder =\> TabularFormBuilder, :lang =\> current\_language}), &proc) end

where can I search for the info you request?

---

<div class="post-metadata">

**Author:** ![ivanpoval](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@ivanpoval](https://discuss.rubyonrails.org/u/ivanpoval)\
**Post date:** [December 22, 2010, 5:25pm UTC](https://discuss.rubyonrails.org/t/select-tag-without-selected-selected-clause/56387/4 "2010-12-22T17:25:02Z")

</div>

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: &nbsp;&nbsp;select("post", "person\_id", Person.all.collect {|p| [p.name, p.id] }, { :include\_blank =\> true }) could become: &nbsp;&nbsp;\<select name="post[person\_id]"\> &nbsp;&nbsp;&nbsp;&nbsp;\<option value=""\>\</option\> &nbsp;&nbsp;&nbsp;&nbsp;\<option value="1" selected="selected"\>David\</option\> &nbsp;&nbsp;&nbsp;&nbsp;\<option value="2"\>Sam\</option\> &nbsp;&nbsp;&nbsp;&nbsp;\<option value="3"\>Tobias\</option\> &nbsp;&nbsp;\</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."
