Country_select box with country code as value

HI all,

I used country select tag for showing country dropdown.

<%= f.country_select(:country, [“United States”]

It result the option values as the full name of that countries as give
below.

Afghanistan Aland Islands Albania Algeria

but I want the country code as option values as below is there any
possible way

Afghanistan Aland Islands Albania Algeria

regards,
Veeraa

I guess then u have to use options_for_select tag and u need to supply
array of countries…

I guess country_select will only allow u to do this

Afghanistan Aland Islands Albania Algeria

Regards,
Ratnavel.

Hi Veera,
I got the following from

http://railsmanual.com/module/ActionView::Helpers::FormOptionsHelper/country_select

but it seems to be down now. Pasted here for your convenience:

  1. Install the tzinfo gem

sudo gem install tzinfo

  1. Install the tzinfo_timezone plugin

ruby script/plugin install tzinfo_timezone

  1. Add the following code to your application helper:

def make_attributes(hsh)
unless hsh.nil?
output = “”
hsh.each do |key, val|
output << " #{key}="#{val}""
end
end
output
end

def country_code_select(object, method, priority_countries=nil,
options={}, html_options={})
countries = [TZInfo::Country.all.collect{|x|x.name},
TZInfo::Country.all_codes].transpose.sort
if priority_countries.nil?
output = select(object, method, countries, options,
html_options)
else
output = “<select id=’#{object}_#{method}’ name=’#{object}
[#{method}]’#{make_attributes html_options}>\n”
if options[:include_blank] == true
output << “”
end
priority_countries.each do |pc|
if options[:selected] == pc[:code] or options[:selected] ==
pc[:name]
output << “#{pc[:name]}\n”
else
output << “#{pc[:name]}
\n”
end
end
output << "----------------------------

\n" output << options_for_select(countries, options[:selected]) output << "\n" end output end
  1. Use the helper like so in your views:

<%= country_code_select(:order, :ship_to_country,
priority_countries=[{:code=>‘US’,:name=>‘United States’}],
{:selected=>‘DK’,:include_blank=>true},{:style=>‘border:10px solid
red;’}) %>

Hope that helps,
Alan

On Apr 17, 7:27 am, Veera S. <rails-mailing-l…@andreas-

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs