gpt4 book ai didi

ruby-on-rails - Activeadmin 表单选择下拉列表更新

转载 作者:行者123 更新时间:2023-12-02 03:04:50 27 4
gpt4 key购买 nike

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

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

最佳答案

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

目录 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

关于ruby-on-rails - Activeadmin 表单选择下拉列表更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26204788/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com