module ActionView::Helpers::FormOptionsHelper

This code was pulled from the state_select gem at github.com/buger/state_select

Constants

AU_NAME
AU_STATES
CA_NAME
CA_STATES
DE_NAME
DE_STATES
ES_NAME
ES_STATES
FR_NAME
FR_STATES
IN_NAME
IN_STATES
MX_NAME
MX_STATES
UG_NAME
UG_STATES
US_NAME
US_STATES

Public Instance Methods

state_options_for_select(selected = nil, country = 'US', options = {:include_blank => true, :optgroup => true}) click to toggle source

Returns a string of option tags for states in a country. Supply a state name as selected to have it marked as the selected option tag.

NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.

# File lib/dm_core/state_select.rb, line 18
def state_options_for_select(selected = nil, country = 'US', options = {:include_blank => true, :optgroup => true})
  state_options = ""
  if country.is_a? Enumerable
    country.each do |each_country|
      #state_options += '<option value=""></option>' if options[:include_blank]
      state_options += "<optgroup label='#{self.class.const_get(each_country.upcase+'_NAME')}'>" if options[:optgroup]
      state_options += options_for_select(self.class.const_get(each_country.upcase+'_STATES'), selected) if each_country
    end
  else
    if country
      #state_options += '<option value=""></option>' if options[:include_blank]
      state_options += "<optgroup label='#{self.class.const_get(country.upcase+'_NAME')}'>" if options[:optgroup]
      state_options += options_for_select(self.class.const_get(country.upcase+'_STATES'), selected)
    end
  end
  return state_options.html_safe
end
state_select(object, method, country='US', options = {}, html_options = {}) click to toggle source

Return select and option tags for the given object and method, using state_options_for_select to generate the list of option tags.

# File lib/dm_core/state_select.rb, line 9
def state_select(object, method, country='US', options = {}, html_options = {})
  ActionView::Helpers::InstanceTag.new(object, method, self, options.delete(:object)).to_state_select_tag(country, options, html_options)
end