Activeadmin表单选择下拉列表更新

时间:2014-10-05 16:37:56

标签: ruby-on-rails ruby-on-rails-4 activeadmin

我有一个类别模型和一个SubCategory模型,ID类似于SubCategory选择要刷新的输入,具体取决于与我选择的特定类别相关联的子类别。

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

3 个答案:

答案 0 :(得分:15)

您可以使用依赖选择来实现此目的。 示例给出了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

目录控制器

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

答案 1 :(得分:0)

您可以执行以下操作以添加特定选择

form do |f|
            f.inputs do
            f.input :category , :as => :select, collection: (['conversion','double-bar', 'fixed-hybrid','removal-bar-over-denture', 'surgical-guides'])
  
            end
            actions 
          end

答案 2 :(得分:-1)

  1. 您必须在ActiveAdmin类别模型中创建一个成员操作(方法:GET,params:所选类别的ID),该模型返回SubCategories(例如,在json中)属于所选类别:< / p>

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

  2. 你必须使用带有ajax的jQuery(例如),当类别选择输入被更改时填充SubCategory选择输入:

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