2012-04-07 60 views
11

我現在有這個作爲搜索:傳遞數組到select_tag

<%= 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列表進入下拉菜單?

回答

23

查看API documentationselect_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 %>