2012-04-07 12 views
11

için geçirilmesi dizisi Şu anda bir arama olarak bu var:bir select_tag

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

benim kullanıcı modeli aşağıdaki listesine sahiptir:

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"] 

merak ediyorum nasıl bütün county_options yapılacaklar listesi açılır menüde?

cevap

23

select_tag için API documentation'a bakın.

Diyor ki

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

seçme kutusu için seçenek etiketlerini içeren bir dizedir. Kapsayıcıları bir dizi seçenek etiketine dönüştüren diğer yardımcı yöntemleri kullanabilirsiniz.

İlk örnek:

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

Bu belirli model verilerinden seçin etiketler oluşturur.

Örneğiniz için options_for_select kullanmalısınız.

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