# How can I set selected value on select\_tag

**URL:** <https://www.ruby-forum.com/t/how-can-i-set-selected-value-on-select-tag/157306>\
**Category:** Rails\
**Created:** [January 9, 2009, 10:04am UTC](https://www.ruby-forum.com/t/how-can-i-set-selected-value-on-select-tag/157306 "2009-01-09T10:04:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [January 9, 2009, 10:04am UTC](https://www.ruby-forum.com/t/how-can-i-set-selected-value-on-select-tag/157306/1 "2009-01-09T10:04:55Z")

</div>

This is my select\_tag code:

\<%=select\_tag ‘project\_version\_select’,  
options\_for\_select(@project\_versions)%\>

How can I set the selected value for this select box?

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [January 9, 2009, 4:19pm UTC](https://www.ruby-forum.com/t/how-can-i-set-selected-value-on-select-tag/157306/2 "2009-01-09T16:19:09Z")

</div>

On Fri, Jan 9, 2009 at 4:04 AM, Zhao Yi  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> This is my select\_tag code:
> 
> \<%=select\_tag ‘project\_version\_select’,  
> options\_for\_select(@project\_versions)%\>
> 
> How can I set the selected value for this select box?

Look at the documentation for #options\_for\_select (see  
[http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M002023&name=options\_for\_select](http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M002023&name=options_for_select)).  
It takes a second argument which specifies which option should be  
selected.

–wpd

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [January 10, 2009, 8:35pm UTC](https://www.ruby-forum.com/t/how-can-i-set-selected-value-on-select-tag/157306/3 "2009-01-10T20:35:56Z")

</div>

Patrick D. wrote:

> ```
> <%=select_tag 'project_version_select',
> options_for_select(@project_versions)%>
> 
> How can I set the selected value for this select box?
> 
> ```
> 
> Look at the documentation for #options\_for\_select (see  
> [http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M002023&name=options\_for\_select](http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M002023&name=options_for_select)  
> [http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M002023&name=options\_for\_select](http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M002023&name=options_for_select)).  
> It takes a second argument which specifies which option should be selected.

Or instead use

@my\_model\_object = look\_it\_up()  
select ‘my\_model\_object’, ‘my\_attribute’,  
my\_map\_of\_strings\_and\_values

the non-\_tag versions of all the helpers are much better because they  
enforce  
cleaner controller actions, and they are aware of your objects and  
members so  
they can make them sticky.

–  
Phlip
