views:

102

answers:

2

i have a database Country filled with (obvious) contries. now i would like to display them in the select_tag in my view (like a drop down list).

i tried puttin in options_for_select sth like @show_me_the_countries_mama.each do |f| ('[' + f.printable_name + ']' + ','). this would list countries each one of them in [] brackets and with spaces in normal view. but it doesnt work in options_for_select for doing the drop down list.

i have:

<% form_tag :method => 'get' do %> Country:<%= select_tag(:country, options_for_select([]))%> <%= submit_tag "Submit" %> <% end %>

how can i solve this? i somehow have to put an array of countries in options_for_select and now i am asking how should i do this. shoud i write separate method in the model for getting the proper array of countries and then inserting them here or..?

thank you for your answers

+4  A: 

Try to use the collection_select tag:

<%= collection_select :object, :country_id, Country.all, :id, :printable_name %>

Where :id and :printable_name are the methods passed to each model in Country.all to get the value and display parts of a select option, respectively.

You could also look at formtastic for very easy form generation.

dvyjones
perfect! thank you again!
A: 

You could use the country_select plugin so that you don't have to maintain any of that in your DB. If you go the formtastic route you'll still need a plugin like country_select.

Chris