RailsCasts Pro episodes are now free!

Learn more or hide this

Flávio M. Oota's Profile

GitHub User: massayoshi

Site: http://www.oota.com.br

Comments by Flávio M. Oota

Avatar

Wondering how could this be implemented using ActiveAdmin or Rails Admin.

Avatar

I was able to "fix" the problem when editing an existing product and not having the second select or having it with all the options. It's looking pretty nasty but it works.

coffee.js
jQuery ->
  loaded_product = $('#product_category_id :selected').text()
  categorys = $('#product_category_id').html()
  $('#product_category_id').parent().hide()
  if loaded_product.length != 0
   brand = $('#product_brand_id :selected').text()
   escaped_brand = brand.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
   options = $(categorys).filter("optgroup[label=#{escaped_brand}]").html()
   $('#product_category_id').html(options)
   $('#product_category_id').parent().show()
  console.log(categorys)
  $('#product_brand_id').change ->
    brand = $('#product_brand_id :selected').text()
    escaped_brand = brand.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(categorys).filter("optgroup[label=#{escaped_brand}]").html()
    console.log(options)
    if options
      $('#product_category_id').html(options)
      $('#product_category_id').parent().show()
    else
      $('#product_category_id').empty()
      $('#product_category_id').parent().hide()
Avatar

The only problem is that when you show the second select, it shows all the options and not only the one it was supposed to.