Skip to content

Instantly share code, notes, and snippets.

@manfe
Created September 11, 2012 20:24
Show Gist options
  • Save manfe/3701739 to your computer and use it in GitHub Desktop.
Save manfe/3701739 to your computer and use it in GitHub Desktop.
Creating <optgroup> in select fields using simple_form
= f.association :cities, :collection => State.all, :as => :grouped_select,
:group_method => :cities, :group_label_method => :name
class City < ActiveRecord::Base
attr_accessible :name
validates :name, :presence => true
belongs_to :state
end
class State < ActiveRecord::Base
attr_accesible :name
validates :name, :presence => true
has_many :cities, :order => :name #this order will show the cities in alphabetical order inside each optgroup
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment