# Collection select options

**URL:** <https://www.ruby-forum.com/t/collection-select-options/182219>\
**Category:** Rails\
**Created:** [January 25, 2010, 12:09pm UTC](https://www.ruby-forum.com/t/collection-select-options/182219 "2010-01-25T12:09:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 25, 2010, 12:09pm UTC](https://www.ruby-forum.com/t/collection-select-options/182219/1 "2010-01-25T12:09:55Z")

</div>

I want to set a default on a collection select. There must be a way to  
do this, but how?

> :default =\> “kdjng” (?)

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 25, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/collection-select-options/182219/2 "2010-01-25T12:23:55Z")

</div>

Pale H. wrote:

> I want to set a default on a collection select. There must be a way to  
> do this, but how?
> 
> > :default =\> “kdjng” (?)

\<%= collection\_select(:category, :id, @categories, :id, :name,  
{},{:size=\>10}) %\>  
will have the value of @category selected  
so you can set the default value to @category and then use the  
collection select.

This was the method I used as I had to make different categories as  
default selected one for different users.There might be some better ways  
too.Eager to know them too.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 25, 2010, 12:36pm UTC](https://www.ruby-forum.com/t/collection-select-options/182219/3 "2010-01-25T12:36:49Z")

</div>

Charanya N. wrote:

> \<%= collection\_select(:category, :id, @categories, :id, :name,  
> {},{:size=\>10}) %\>  
> will have the value of @category selected  
> so you can set the default value to @category and then use the  
> collection select.
> 
> This was the method I used as I had to make different categories as  
> default selected one for different users.There might be some better ways  
> too.Eager to know them too.

Thanks for your response.

> > \<%= f.collection\_select :brand, [‘Brand 1’, ‘Brand 2’, ‘Brand 3’, ‘Brand 4’], :to\_s, :to\_s %\>

The above is the syntax I’m currently using. In theory, the default  
brand would be Brand 1, correct? So when a form containing this  
collection gets submitted (without amending the collection), Brand 1 is  
parsed as the one selected. At least, that’s what I’m looking for.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 25, 2010, 1:16pm UTC](https://www.ruby-forum.com/t/collection-select-options/182219/4 "2010-01-25T13:16:07Z")

</div>

If you want to have a default selected value then you can use select\_tag

\<%= select\_tag :brand, options\_for\_select([‘Brand 1’, ‘Brand 2’, ‘Brand  
3’, ‘Brand 4’], “Brand 2”) %\>

You had used this  
\<%= f.collection\_select :brand, [‘Brand 1’, ‘Brand 2’, ‘Brand 3’, ‘Brand  
4’], :to\_s, :to\_s %\>

in case if the form f object is for article then u can use

\<%= select\_tag ‘article[brand]’, options\_for\_select([‘Brand 1’, ‘Brand  
2’, ‘Brand 3’, ‘Brand 4’], “Brand 2”) %\>

Hope this helps.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [January 25, 2010, 1:19pm UTC](https://www.ruby-forum.com/t/collection-select-options/182219/5 "2010-01-25T13:19:03Z")

</div>

Charanya N. wrote:

> If you want to have a default selected value then you can use select\_tag
> 
> \<%= select\_tag :brand, options\_for\_select([‘Brand 1’, ‘Brand 2’, ‘Brand  
> 3’, ‘Brand 4’], “Brand 2”) %\>
> 
> You had used this  
> \<%= f.collection\_select :brand, [‘Brand 1’, ‘Brand 2’, ‘Brand 3’, ‘Brand  
> 4’], :to\_s, :to\_s %\>
> 
> in case if the form f object is for article then u can use
> 
> \<%= select\_tag ‘article[brand]’, options\_for\_select([‘Brand 1’, ‘Brand  
> 2’, ‘Brand 3’, ‘Brand 4’], “Brand 2”) %\>
> 
> Hope this helps.

Thank you 🙂
