# "selected" in form.select

**URL:** <https://www.ruby-forum.com/t/selected-in-form-select/106093>\
**Category:** Rails\
**Created:** [June 6, 2007, 10:47am UTC](https://www.ruby-forum.com/t/selected-in-form-select/106093 "2007-06-06T10:47:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [June 6, 2007, 10:47am UTC](https://www.ruby-forum.com/t/selected-in-form-select/106093/1 "2007-06-06T10:47:28Z")

</div>

I have a search form in my navbar where a user can select in which  
area he wants to search. After clicking the submit-button the  
results get rendered but the previous selected area is no longer  
selected.

\<%= form.select (:suchfeld,  
[[‘Beschreibungen’,‘description’],  
[‘Headlines’,‘headline’]] )  
%\>

I was able to re-fill the search-field wich the previous content  
doing the following:

\<%= form.text\_field  
:suche,  
:name =\> “suche”,  
:size =\> 25,  
:value =\> params[:suche].gsub(’ ‘,’’)  
%\>

But how can I automatically add “selected” to a select-field to  
select the last selected item?

Thanx

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [July 23, 2007, 12:42am UTC](https://www.ruby-forum.com/t/selected-in-form-select/106093/2 "2007-07-23T00:42:20Z")

</div>

The easiest way is to use the html attrs parameter, like this:  
\<%= form.select (:suchfeld,

> [[‘Beschreibungen’,‘description’],  
> [‘Headlines’,‘headline’]]  
> , :selected =\> @varname  
> )  
> %\>

where “@varname” is the value that was selected before posting. That  
var would be set in the controller, probably when you are grabbing the  
post data.
