0

I have a dropdown with US states and territories. I would like to add Canadian provinces so that the entire list is states/provinces in alphabetical order.

Currently I have this code which lists all US states and territories:

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

I tried converting the second parameter of subregion_select to ["US, "CA"] but that breaks things.

Mike Glaz
  • 5,352
  • 8
  • 46
  • 73

1 Answers1

1

As per my understanding you are looking for union of Canadian provinces and US states for a select field without country_select functionality. If am i right, you can get by this way

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 

In rails application

Create helper method

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

Call the above method in 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'

I hope this would be helpfull

Nitin Srivastava
  • 1,414
  • 7
  • 12
  • I don't understand where `label_method: :name, value_method: :code` come from and what they do? – Mike Glaz Jan 22 '16 at 20:35
  • I hope you are using `simple_form`. `label_method` and `value_method` come with `simple_form`. The `lebel_method` for `` and `value_method` for ``. So if your `subregion` name is `Alabama` and code is `AL` then it becomes ``. For more details please inspect you `select` field and see their `options`. – Nitin Srivastava Jan 22 '16 at 20:45