2016-01-22 42 views
0

我有一個與美國各州和地區的下拉菜單。我想添加加拿大省份,以便整個列表按照字母順序排列在州/省。卡門鐵路分區域選擇(結合加拿大各省和美國州)

目前我有這樣的代碼,其中列出美國所有州和領地:

= extra_fields.input :province, label: "Franchisee Billing State/Province", input_html: { class: "form-control" } do 
    = extra_fields.subregion_select(:province, "US", {prompt: 'Please select a state'}, required: 'region required') 

我試過subregion_select的第二個參數轉換爲[「美國,‘CA’]但是,打破東西

回答

1

按我的理解,你是不country_select功能尋找加拿大各省和美國各州的select場的結合。如果我是正確的,你可以通過這種方式

countries = Carmen::Country.all.select{|c| %w{US CA}.include?(c.code)} # get the countries 
# get the subregions of US and CA 
subregions = countries.collect {|x| x.subregions }.flatten 
獲取

在Rails應用程序

創建的helper方法

def subregions_of_us_and_canada 
    countries = Carmen::Country.all.select{|c| %w{US CA}.include?(c.code)} 
    # get subregions and sort in alphabetical order 
    countries.collect {|x| x.subregions }.flatten.sort_by(&:name) 
end 

以電話形式的上述方法

= extra_fields.input :province, as: :select, collection: subregions_of_us_and_canada, label_method: :name, value_method: :code, label: "Franchisee Billing State/Province", input_html: { class: "form-control" }, prompt: 'Please select a state' 

我希望這將是有益的

+0

我不明白其中'label_method :: name,value_method :: code'來了從他們做什麼? – mikeglaz

+0

我希望你使用'simple_form'。 'label_method'和'value_method'帶有'simple_form'。 ''和'value_method'的'lebel_method'爲''。所以如果你的'subregion'的名字是'Alabama'並且代碼是'AL',那麼它就變成''。欲瞭解更多詳情,請檢查你'選擇'字段,並看到他們的'選項'。 –

相關問題