gpt4 book ai didi

ruby-on-rails - 将数组传递给 select_tag

转载 作者:行者123 更新时间:2023-12-04 00:22:21 25 4
gpt4 key购买 nike

我目前有这个作为搜索:

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

我的用户模型中有以下列表:
  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"]

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

最佳答案

查看 API documentation对于 select_tag .

它说:

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

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

第一个例子:
select_tag "people", options_from_collection_for_select(@people, "id", "name")
# <select id="people" name="people"><option value="1">David</option></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 %>

关于ruby-on-rails - 将数组传递给 select_tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10056397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com