Active Admin表单上的动态调整下拉字段(Rails 3.2) [英] Dynamically-adjusting drop down fields on Active Admin form (rails 3.2)
问题描述
我在Ruby on Rails应用程序上有一个非常基本的体系结构:
I have a very basic architecture on my Ruby on Rails app:
-
公司模型
a company model
一个行业模型(公司属于一个行业)
a Industry model (a company belongs to an industry)
a子行业模型(子行业属于
a Sub-industry model (a subindustry belongs to a Industry)
在我的活动管理表单页面上,我想输入字段,以便对每个公司来说首先从下拉列表中选择所属行业(到目前为止有效),然后选择另一个字段子行业,根据我在上一个字段中选择的行业,该字段将仅显示我之前已分类的子行业
On my active admin form page, I want to put fields so that for each company, I first select in a drop down the industry it belongs to (this works so far), and then another field "sub industry", that would ONLY display, according to the industry I selected in the previous field, the subindustries that I have categorized previously in this industry.
例如,古柯将进入饮料和饮料行业,我希望表格能够动态调整,仅在 sub字段中显示行业:带有热饮,冷饮,茶,苏打水的下拉列表
For example, coca would go in industry "Drinks and beverage" and I'd like the form to dynamically adjust and only show in the field "sub indsutry" :a dropdown with "hot drinks", "cold drinks", "tea", "sodas"
form do |f|
f.inputs "Company" do
f.input :company_industry_id, :label => "Select industry:", :as => :select, :collection => CompanyIndustry.all
f.input :company_subindfustry_id, :label => "Select subindustry:", :as => :select, :collection => CompanySubindustry.all
end
到目前为止,我显然有问题,它向我显示了全部我拥有的子行业,而不仅是我在上一个字段中选择的行业内的子行业。
Obviously I have a problem as so far, it show me ALL the subindustries I have and not only the subindustries WITHIN the industry I select on the previous field.
有人知道我能解决这个问题吗?
Does anyone know how I can solve that ?
推荐答案
您需要执行页面刷新以根据所选内容更新数据,或者(我个人偏好)执行AJAX调用以更新其他下拉列表。
You'll need to do either a page refresh to update data based on what's selected or (my preference) an AJAX call to update the other dropdown after the first one is selected.
有一个很棒的 railscast 就是这样做的-那将是一个不错的起点。
There's a great railscast on doing just this - that would be a great place to start.
这篇关于Active Admin表单上的动态调整下拉字段(Rails 3.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!