Skip to content

Instantly share code, notes, and snippets.

@tubbo
Created January 30, 2015 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tubbo/c85289ff272e8aa624f6 to your computer and use it in GitHub Desktop.
Save tubbo/c85289ff272e8aa624f6 to your computer and use it in GitHub Desktop.
/config/routes.rb
------------------------------------------------------------------------
get '/products/update_productsubgroups', as: 'update_productsubgroups'
get '/update_productsubgroups' => 'products#update_productsubgroups'
resources :prodducts
/app/assets/javascript/products.coffee
------------------------------------------------------------------------
$ ->
$(document).on 'change', '#productgroups_select', (evt) ->
$.ajax 'update_productsubgroups',
type: 'GET'
dataType: 'script'
data: {
productgroup_id: $("#productgroups_select option:selected").val()
}
error: (jqXHR, textStatus, errorThrown) ->
console.log("AJAX Error: #{textStatus}")
success: (data, textStatus, jqXHR) ->
console.log("Dynamic country select OK!")
/app/views/products/new.html.erb (cascading select)
- dropdown code is the same for views/products/edit.html
------------------------------------------------------------------------
<%= f.label :productgroup, "Group", :class => 'col-sm-3 control-label' %>
<div class="col-sm-9">
<%= f.select :productgroup_id, options_for_select( @productgroups.collect { |productgroup|
[productgroup.name.titleize, productgroup.id] },
@product.productgroup_id), {} ,
{ class: 'form-control', id: 'productgroups_select' } %>
</div>
<%= f.label :productsubgroup, "Sub-group", :class => 'col-sm-3 control-label' %>
<div class="col-sm-9">
<%= f.select :productsubgroup_id, options_for_select( @productsubgroups.collect { |productsubgroup|
[productsubgroup.name.titleize, productsubgroup.id] },
@productsubgroupschoice), {include_blank: "--Please Select--"} ,
{ class: 'form-control', id: 'productsubgroups_select' } %>
</div>
/app/views/products/update_productsubgroups.html.erb
------------------------------------------------------------------------
$("#productsubgroups_select").empty()
.append("<%= escape_javascript(render(:partial => @productsubgroups)) %>")
/app/views/produtsubgroups/_productsubgroups.html.erb
------------------------------------------------------------------------
<option value="<%= productsubgroup.id %>"><%= productsubgroup.name.titleize %></option>
resources :products do
collection do
get :update_productsubgroups
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment