carmen-rails 次区域 select(合并加拿大各省和美国各州)

carmen-rails subregion select (combine Canadian provinces and US states)

我有一个美国各州和领地的下拉列表。我想添加加拿大省份,以便整个列表按字母顺序 states/provinces。

目前我有这个列出美国所有州和地区的代码:

= 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"] 但这会破坏一些东西。

据我了解,您正在寻找加拿大各省和美国各州的联合,以获得没有 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申请中

创建辅助方法

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

在form中调用上面的方法

= 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'

希望对您有所帮助