Select_tag and selected value again

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?

Hi Remco

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

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

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

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

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

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

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

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)) %>

Chris B.:

<%= select_tag( “location”,
options_from_collection_for_select(Location.find(:all), 2)) %>

have you tried to run it? you will get error :slight_smile:

Reinhart
http://teapoci.blogspot.com

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-