Skip to content

Instantly share code, notes, and snippets.

@joseh-henrique
Forked from manfe/_some_field.html.haml
Created January 6, 2017 22:33
Show Gist options
  • Save joseh-henrique/83e26e95cca18ef9995b2f3ed080f592 to your computer and use it in GitHub Desktop.
Save joseh-henrique/83e26e95cca18ef9995b2f3ed080f592 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