2014-10-05 55 views
8

我有一个类别模型和一个子类别模型和id像SubCategory选择输入来刷新,取决于与我选择的特定类别关联的SubCategories。Activeadmin表格选择下拉更新

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 

回答

0
  1. 你必须创建一个构件的动作(方法:GET,则params:所选择的类别的ID)中,返回的子类别(在JSON,例如)的ActiveAdmin类别模型属于选择类别:

    https://github.com/activeadmin/activeadmin/blob/master/docs/8-custom-actions.md#member-actions

  2. 你必须使用jQuery(对于〔实施例)与阿贾克斯,即填充子类别选择输入时,类别选择输入已更改:

    Populate Select box options on click with Javascript/Jquery with Json data https://forum.jquery.com/topic/best-practices-for-populating-selects-with-ajax

12

您可以使用依赖于选择用于此目的。 例子说明here

主动联系

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 

目录控制器

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 
+3

如果你懒,只是想使串试试这个:'f.input:状态,如:选择,集合:([“new”,“checked_in”,“called”,“complete”])'' – michael 2016-04-21 13:46:43