options_from_collection_for_select

225 views
Skip to first unread message

Shandy Nantz

unread,
Apr 2, 2008, 5:28:10 PM4/2/08
to rubyonra...@googlegroups.com
I have a drop down of country's and I would like the USA to be the first
displayed. I have tried:

<select id = "user_w_country" name ="user[w_country]" onchange =
"<%=func%>">
<%= options_from_collection_for_select @country, 'id',
'country', :selected => Countrycode.find_by_country('USA').id %>
</select>

It doesn't complai9n about the code but it doesn't work either. The
documentation for the options_from_collection_for_select says the third
argument is a select option, but I am obviously doing something wrong.
Any ideas? Thanks,

-S
--
Posted via http://www.ruby-forum.com/.

Shandy Nantz

unread,
Apr 4, 2008, 9:56:38 AM4/4/08
to rubyonra...@googlegroups.com
Can anyone tell me if am putting the select in the right place?

AndyV

unread,
Apr 5, 2008, 11:39:37 AM4/5/08
to Ruby on Rails: Talk
If you're going to use options_from_collection_for_select it's
probably also worth using the other rails helpers as well.

<%= select_tag "user_w_country", @country, :id, :country,
Countrycode.find_by_country('USA') %>
<%= observe_field 'user_w_country', :function => func %>


HTH,
AndyV

On Apr 4, 9:56 am, Shandy Nantz <rails-mailing-l...@andreas-s.net>
wrote:

Shandy Nantz

unread,
Apr 7, 2008, 5:53:53 PM4/7/08
to rubyonra...@googlegroups.com
I have this collection and I am trying to display from the list which
one I would like to have selected when I am rendering a dropdown. Here
is what I got:

<%= options_from_collection_for_select @country, 'id', 'country',

:selected => 'USA' %>

Displays the list fine (this is a dropdown of country's by the way).
What I would like it to do is pick out USA a the selected option when
you drop down the select box, but so far nothin'. Has anyone done this?
What do I have to do to select an entry from the
options_from_collection_for_select? Thanks,

-S

Julian Leviston

unread,
Apr 8, 2008, 12:13:35 AM4/8/08
to rubyonra...@googlegroups.com
Hi,

That doesn't make a WHOLE lot of sense.

options_from_collection_for_select will give you <option> tags. It
takes a collection, and then the value and display value methods to
send to the objects in the collection as it traverses them, creating
the option tags.

Thus, @country, here, would be assumedly a list of country objects
which take the id and country methods as their value and display
values respectively.

There is an optional fourth value, which is the VALUE to match with
the object you want.

For instance:

<%= options_from_collection_for_select @country, 'id', 'country',

@selected_country %>

where @selected_country is the id of the country you want selected.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/

Reply all
Reply to author
Forward
0 new messages