Ruby-on-rails 将 Array 传递给 select_tag

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10056397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:11:14  来源:igfitidea点击:

Passing Array to a select_tag

ruby-on-railsselect

提问by user1222136

I currently have this as a search:

我目前有这个作为搜索:

 <%= form_tag users_path, :controller => 'users', :action => 'townsearch', :method => 'get' do %>
    <%= select_tag :county, params[:county] %>
    <%= submit_tag 'search'%>
 <% end %>

I have the following list in my user model:

我的用户模型中有以下列表:

  COUNTY_OPTIONS = [ "Avon", "Bedfordshire", "Berkshire", "Borders", "Buckinghamshire", "Cambridgeshire","Central",
                 "Cheshire", "Cleveland", "Clwyd", "Cornwall", "County Antrim", "County Armagh", "County Down",
                 "County Fermanagh", "County Londonderry", "County Tyrone", "Cumbria", "Derbyshire", "Devon",
                 "Dorset", "Dumfries and Galloway", "Durham", "Dyfed", "East Sussex", "Essex", "Fife", "Gloucestershire", 
                 "Grampian", "Greater Manchester", "Gwent", "Gwynedd County", "Hampshire", "Herefordshire", "Hertfordshire",
                 "Highlands and Islands", "Humberside", "Isle of Wight", "Kent", "Lancashire", "Leicestershire", "Lincolnshire",
                 "Lothian", "Merseyside", "Mid Glamorgan", "Norfolk", "North Yorkshire", "Northamptonshire", "Northumberland",
                 "Nottinghamshire", "Oxfordshire", "Powys", "Rutland", "Shropshire", "Somerset", "South Glamorgan", "South Yorkshire",
                 "Staffordshire", "Strathclyde", "Suffolk", "Surrey", "Tayside", "Tyne and Wear", "Warwickshire", "West Glamorgan",
                 "West Midlands", "West Sussex", "West Yorkshire", "Wiltshire", "Worcestershire"]

I am wondering how do I all the county_options list in to the drop down menu?

我想知道如何将所有 County_options 列表添加到下拉菜单中?

回答by shime

Check out the API documentationfor select_tag.

退房的API文档select_tag

It says:

它说:

select_tag(name, option_tags = nil, options = {}) 

Where option_tagsis a string containing the option tags for the select box. You can use other helper methods that turn containers into a string of option tags.

哪里option_tags是包含选择框选项标签的字符串。您可以使用其他辅助方法将容器转换为选项标签字符串。

First example:

第一个例子:

select_tag "people", options_from_collection_for_select(@people, "id", "name")
# <select id="people" name="people"><option value="1">David</option></select>

This generates select tags from specific model data.

这会从特定模型数据生成选择标签。

For your example you should use options_for_select.

对于您的示例,您应该使用options_for_select.

<%= form_tag users_path, :controller => 'users', :action => 'townsearch', :method => 'get' do %>
    <%= select_tag :county, options_for_select(User::COUNTY_OPTIONS) %>
    <%= submit_tag 'search'%>
 <% end %>

回答by Marc Gagne