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?

Remco Hh 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.


@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.


"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:

CORRECTION <%= select_tag "location","<option value=\"#{}\"> #{}</option>"+ options_from_collection_for_select(@locations-@selected.to_a, :id, :name) %>

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 Barnes:

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

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


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