Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activeadmin form select dropdown update

I have a Category Model and a SubCategory Model and id like the SubCategory select input to refresh depending on the SubCategories that are associated to the specific Category I have selected.

form do |f|
    f.inputs do
        f.input :title
        f.input :category, as: :select, collection: Category.all, :input_html => { :class => 'chzn-select', :width => 'auto', "data-placeholder" => 'Click' }
        f.input :sub_category, as: :select, collection: SubCategory.all, :input_html => { :class => 'chzn-select', :width => 'auto', "data-placeholder" => 'Click' }
    end
    f.actions
end
like image 782
Dan Mitchell Avatar asked Oct 05 '14 16:10

Dan Mitchell


1 Answers

You can use dependent select for this purpose. Example is given here

Active Admin

ActiveAdmin.register CatalogsProduct do
  form do |f|
    f.inputs "Details" do
      f.input :product, :as => :select, :collection => Product.all.collect {|product| [product.name, product.id] }
      f.input :catalog, :as => :select, :input_html => {'data-option-dependent' => true, 'data-option-url' => '/products/:catalogs_product_product_id/catalogs', 'data-option-observed' => 'catalogs_product_product_id'}, :collection => (resource.product ? resource.product.category.catalogs.collect {|catalog| [catalog.attr_name, catalog.id]} : []) 
    end
    f.actions
  end
end

Catalog controller

class CatalogsController < ApplicationController
  respond_to :json

  def index
    if params[:product_id]
      product = Product.find_by_id(params[:product_id])
      @catalogs = product.category.catalogs
    else
      @catalogs = Catalog.all
    end
    render :json => @catalogs.collect {|catalog| {:id => catalog.id, :name => catalog.attr_name} }
  end
end
like image 103
eshaiju Avatar answered Oct 03 '22 08:10

eshaiju