Created
September 11, 2012 20:24
-
-
Save manfe/3701739 to your computer and use it in GitHub Desktop.
Creating <optgroup> in select fields using simple_form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= f.association :cities, :collection => State.all, :as => :grouped_select, | |
:group_method => :cities, :group_label_method => :name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class City < ActiveRecord::Base | |
attr_accessible :name | |
validates :name, :presence => true | |
belongs_to :state | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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