Reputation: 5126
I'm using a Ruby on Rails gem called simple_form with country_select and I'm trying to get the drop down to display the long country name but set the value to the short iso code.
If I have the following
= f.input :country, priority: ["Australia", "United States", "New Zealand"]
Then all the countries after my priority countries are correct (displaying the fullname whilst using the iso_code for the value). The priority countries though use the name as the label & value. Is there a way to set the ISO code on the priority countries?
Upvotes: 2
Views: 3015
Reputation: 2270
Just pass iso_codes: true
to your field:
= f.input :country, priority: ["Australia", "United States", "New Zealand"], iso_codes: true
Tested with simple_form 3.1.0.rc2 and country_select 1.3.1
Upvotes: 1
Reputation: 289
You can set
::CountrySelect.use_iso_codes = true
in an initializer to globally use the ISO codes as dropdown values instead of country names.
http://rubydoc.info/gems/country_select/1.3.1/frames
Upvotes: 2
Reputation: 18845
Looking into simple_form source is a real PITA because it is so incredible abstract. But from what I can see, this seems to be an issue with using the CountrySelect Gem.
The gem says that you should use iso_codes: true
to make sure the code is used as a key. I guess that you will have to digg into how the priority
parameters get handled or try passing this option through to the gem.
Here is the relevant code:
def country_options_for_select(selected = nil, priority_countries = nil, use_iso_codes = false)
country_options = "".html_safe
if priority_countries
priority_countries_options = if use_iso_codes || ::CountrySelect.use_iso_codes
priority_countries.map do |code|
[
::CountrySelect::COUNTRIES[code],
code
]
end
else
priority_countries
end
country_options += options_for_select(priority_countries_options, selected)
country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n".html_safe
#
# prevents selected from being included
# twice in the HTML which causes
# some browsers to select the second
# selected option (not priority)
# which makes it harder to select an
# alternative priority country
#
selected = nil if priority_countries.include?(selected)
end
values = if use_iso_codes || ::CountrySelect.use_iso_codes
::CountrySelect::ISO_COUNTRIES_FOR_SELECT
else
::CountrySelect::COUNTRIES_FOR_SELECT
end
return country_options + options_for_select(values.sort, selected)
end
Upvotes: 0