开发者

how to put elements from model into select_tag

开发者 https://www.devze.com 2022-12-14 22:27 出处:网络
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 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:

'get' do %> Country:

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

开发者_Python百科

thank you for your answers


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消