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 的第二个参数转换为 ["US, "CA"] 但这会破坏事情。

4

1 回答 1

1

据我了解,您正在为一个select没有country_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 

在轨道应用中

创建辅助方法

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'

我希望这会有所帮助

于 2016-01-22T20:11:57.930 回答