Skip to content

Instantly share code, notes, and snippets.

@johnmeehan
Created November 1, 2016 11:26
Show Gist options
  • Save johnmeehan/d4a2f6c375f4106a6357cb8706dc2d4e to your computer and use it in GitHub Desktop.
Save johnmeehan/d4a2f6c375f4106a6357cb8706dc2d4e to your computer and use it in GitHub Desktop.
Grouped Select Example
# :coffeescript
jQuery ->
designs = $('.design').html()
$('.style').change ->
style = $('.style option:selected').text()
options = $(designs).filter("optgroup[label='" + style + "']").html()
$('.design').html(options)
-# scopes the designs based on the style choice. using simple_form grouped_select
.row
.col-md-3
= f.input :style_id, collection: Style.glazed_panels.options_for_select, input_html: {class: 'form-control style'}, include_blank: false
.row
.col-md-3
.form-group
-# = f.input(:design_id, collection: RenaissanceDesign.options_for_select, input_html: {class: 'form-control design'}, include_blank: false )
= f.input :design_id, collection: Style.glazed_panels, as: :grouped_select, group_method: :designs, input_html: {class: 'form-control design'}
# Models and associations.
class RenaissanceDesign < ActiveRecord::Base
belongs_to :style
end
class Style < ActiveRecord::Base
has_many :designs, class_name: 'RenaissanceDesign'
scope :glazed_panels, -> { where('......') }
scope :options_for_select, -> {order(:name).pluck(:name, :id)}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment