# Select\_tag and selected value again

**URL:** <https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041>\
**Category:** Rails\
**Created:** [April 28, 2008, 11:33am UTC](https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041 "2008-04-28T11:33:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [April 28, 2008, 11:33am UTC](https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041/1 "2008-04-28T11:33:30Z")

</div>

Many posts I found about this, and many possible solutions, but i just  
can’t get this simple thing to work: i tried every single solution i  
found:

\<%=select\_tag  
“location”,options\_from\_collection\_for\_select(Location.find(:all), :id,  
:name, “2”)%\>

\<%=select\_tag  
“location”,options\_from\_collection\_for\_select(Location.find(:all), :id,  
:name, 2)%\>

\<%=select\_tag  
“location”,options\_from\_collection\_for\_select(Location.find(:all), :id,  
:name, {:selected=\>“2”})%\>

\<%=select\_tag  
“location”,options\_from\_collection\_for\_select(Location.find(:all), :id,  
:name, {:selected=\>2})%\>

\<%=select\_tag  
“location”,options\_from\_collection\_for\_select(Location.find(:all), :id,  
:name, {:selected\_value=\>“2”})%\>

is there anybody out there who knows how to do this?

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [April 28, 2008, 11:38am UTC](https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041/2 "2008-04-28T11:38:14Z")

</div>

Hi Remco

Is that a typo and there is a spcae between \<%= and select\_tag in your  
code?

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [April 28, 2008, 11:56am UTC](https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041/3 "2008-04-28T11:56:52Z")

</div>

I add an additional advice. Remco you should remember that RoR is  
designed with MVC. I disagree if you put Location.find(:all) in your  
view. Put it in your controller as instance variable, then put your  
instance variable into your view.

Controller:

@locations = Location.find(:all)

View :  
\<%= select\_tag  
“location”,options\_from\_collection\_for\_select(@locations, :id,  
:name, “2”) %\>

Feel the taste of ROR by Optimizing MVC. Good luck.

Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

“Misguided in programming and paradigm?”  
“Ruby and Rails is your appropriate map”

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [April 28, 2008, 11:59am UTC](https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041/4 "2008-04-28T11:59:30Z")

</div>

I am aware of that, but i am afraid that doesn’t solve my problem.

Visit Indonesia 2008 wrote:

> I add an additional advice. Remco you should remember that RoR is  
> designed with MVC. I disagree if you put Location.find(:all) in your  
> view. Put it in your controller as instance variable, then put your  
> instance variable into your view.
> 
> Controller:
> 
> @locations = Location.find(:all)
> 
> View :  
> \<%= select\_tag  
> “location”,options\_from\_collection\_for\_select(@locations, :id,  
> :name, “2”) %\>
> 
> Feel the taste of ROR by Optimizing MVC. Good luck.
> 
> Reinhart  
> [http://teapoci.blogspot.com](http://teapoci.blogspot.com)
> 
> “Misguided in programming and paradigm?”  
> “Ruby and Rails is your appropriate map”

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [April 28, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041/5 "2008-04-28T12:41:41Z")

</div>

Controller:

@locations = Location.find(:all)  
@selected = Location.find(2)

View :  
\<%= select\_tag  
“location”,“\<option value="@selected.id"\>  
@selected.name”+  
options\_from\_collection\_for\_select(@removed\_email\_address@domain.invalid\_a, :id,  
:name) %\>

what do you think?

Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [April 28, 2008, 12:43pm UTC](https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041/6 "2008-04-28T12:43:37Z")

</div>

CORRECTION  
\<%= select\_tag  
“location”,“\<option value="#{@selected.id}"\>  
#{@selected.name}”+  
options\_from\_collection\_for\_select(@removed\_email\_address@domain.invalid\_a, :id,  
:name) %\>

> Reinhart  
> [http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [April 28, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041/7 "2008-04-28T15:08:55Z")

</div>

I think you will need to put the select\_tag in parentheses like this.

\<%= select\_tag( “location”,  
options\_from\_collection\_for\_select(Location.find(:all), 2)) %\>

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [April 28, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041/8 "2008-04-28T15:34:44Z")

</div>

Chris B.:

> \<%= select\_tag( “location”,  
> options\_from\_collection\_for\_select(Location.find(:all), 2)) %\>

have you tried to run it? you will get error 🙂

Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [May 2, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/select-tag-and-selected-value-again/136041/9 "2008-05-02T15:43:30Z")

</div>

Sorry no I didn’t run it but the source of the error is easily fixed.  
Lookup options\_from\_collection\_for\_select in the docs.

\<%=  
select\_tag(  
“location”,options\_from\_collection\_for\_select(Location.find(:all),  
“id”, “name”, 3)) %\>

On Apr 28, 9:34 am, Visit Indonesia 2008 \<rails-mailing-l…@andreas-
