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.

<option value="Afghanistan">Afghanistan</option> <option value="Aland Islands">Aland Islands</option> <option value="Albania">Albania</option> <option value="Algeria">Algeria</option>

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

<option value="AF">Afghanistan</option> <option value="FI">Aland Islands</option> <option value="AL">Albania</option> <option value="DZ">Algeria</option>

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

<option value="Afghanistan">Afghanistan</option> <option value="Aland Islands">Aland Islands</option> <option value="Albania">Albania</option> <option value="Algeria">Algeria</option>

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

2. Install the tzinfo_timezone plugin # ruby script/plugin install tzinfo_timezone

3. 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 << "<option value=''></option>"       end       priority_countries.each do |pc|         if options[:selected] == pc[:code] or options[:selected] == pc[:name]           output << "<option value='#{pc[:code]}'

#{pc[:name]}</option>\n"

        else           output << "<option value='#{pc[:code]}'>#{pc[:name]}</option> \n"         end       end       output << "<option value='' disabled>---------------------------- </option>\n"       output << options_for_select(countries, options[:selected])       output << "</select>\n"     end     output   end

4. 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