[英]How to fill other form input when an item from dropdown has been selected in Ruby on Rails using Active Admin
I'm a newbie using Ruby on Rails and ActiveAdmin to develop a project: 我是使用Ruby on Rails和ActiveAdmin开发项目的新手:
I have two models called DirectPurchase and Product. 我有两个模型,分别称为DirectPurchase和Product。 Table migrations look like this:
表迁移如下所示:
Products: 产品:
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :item_code
t.string :item_name
t.string :item_type
t.string :central_name
t.string :origin
t.string :class_type
t.decimal :unit_cost, precision: 8, scale: 2
t.string :liens
t.timestamps null: false
end
end
end
Direct Purchase: 直接购买:
class CreateDirectPurchases < ActiveRecord::Migration
def change
create_table :direct_purchases do |t|
t.string :supplier
t.string :product
t.integer :quantity
t.decimal :unit_cost, precision: 10, scale: 2
t.decimal :commission_rate, precision: 10, scale: 2
t.decimal :total_unit_cost, precision: 10, scale: 2
t.decimal :misc_fees_liens, precision: 10, scale: 2
t.decimal :misc_fees_insurance, precision: 10, scale: 2
t.decimal :misc_fees_storage, precision: 10, scale: 2
t.decimal :misc_fees_penalty, precision: 10, scale: 2
t.decimal :witholding_tax, precision: 10, scale: 2
t.decimal :commission_amount, precision: 10, scale: 2
t.decimal :payable_gross, precision: 10, scale: 2
t.decimal :payable_net, precision: 10, scale: 2
t.string :reference_number
t.timestamps null: false
end
end
end
Now what I want to do is on the new Direct Purchase form: 现在,我要做的是在新的“直接购买”表单上:
![direct purchase form][1] ![直接购买表格] [1]
The product input field is auto-populated by the existing products that have been entered in the past. 产品输入字段由过去输入的现有产品自动填充。
Now when I select a certain product from the dropdown list, I want the unit_cost of the product that I selected to be automatically be filled into the unit_cost field of the direct purchase form. 现在,当我从下拉列表中选择某个产品时,我希望将我选择的产品的unit_cost自动填写到直接购买表单的unit_cost字段中。
How to do this? 这个怎么做? My clue is to use :onchange and :input_html by as I said I'm still a newbie and don't have a clue how to do this.
我的提示是使用:onchange和:input_html,正如我所说的,我仍然是新手,不知道如何执行此操作。
/admin/direct_purchase.rb /admin/direct_purchase.rb
ActiveAdmin.register DirectPurchase do
menu parent: "Transaction"
permit_params :supplier, :product, :quantity, :unit_cost, :commission_rate,
:total_unit_cost, :misc_fees_liens, :misc_fees_insurance, :misc_fees_storage, :misc_fees_penalty,
:witholding_tax, :commission_amount, :payable_gross, :payable_net, :reference_number
index do
selectable_column
column :supplier
column :product
column :quantity
column :unit_cost
column :commission_rate
column :total_unit_cost
column :misc_fees_liens
column :misc_fees_insurance
column :misc_fees_storage
column :misc_fees_penalty
column :witholding_tax
column :commission_amount
column :payable_gross
column :payable_net
column :reference_number
actions
end
form do |f|
f.inputs "Direct Purchase Details" do
f.input :supplier
f.input :product, :as => :select, :collection => Product.all.map {|product| [product.item_name, product.unit_cost]}, :input_html => {:onchange => "SOMETHING HERE"}
f.input :quantity
f.input :unit_cost, :input_html => {:value => "unit_cost value from selected product"}
f.input :commission_rate
f.input :total_unit_cost, :input_html => { :readonly => true }
f.input :misc_fees_liens
f.input :misc_fees_insurance
f.input :misc_fees_storage
f.input :misc_fees_penalty
f.input :witholding_tax
f.input :commission_amount
f.input :payable_gross
f.input :payable_net
f.input :reference_number
end
f.actions
end
end
Any help is appreciated. 任何帮助表示赞赏。
You need to use jQuery. 您需要使用jQuery。 Create a js file in the assets/javascript forlder:
在asset / javascript forlder中创建一个js文件:
$("input[name='direct_purchase[product]']").change(function() {
$("input[name='direct_purchase[unit_cost]']").val($(this).val());
});
Or something similar... 或类似的东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.