简体   繁体   English

当使用Active Admin在Ruby on Rails中选择了下拉菜单中的项时,如何填充其他表单输入

[英]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.

相关问题 从rails的下拉菜单中选择一项后,如何更新数据库字段? - How to update database field when an item has been selected from drop down menu in rails? Ruby on Rails-在单击按钮时从Javascript下拉列表中选择的项目传递给Rails控制器 - Ruby on Rails - Pass item selected from a Javascript dropdown to Rails controller when a button is clicked 如何在活动管理员中获取 form.input 的值(ruby on rails) - How do I get the value of a form.input in active admin (ruby on rails) 如何使用其他模型的数据在Rails中填写表单时发送params - How to send params when using data from other models to fill in the form in Rails 如何在 ruby 活动管理表单中动态获取输入字段的值 - How to dynamically get value of input fields in ruby active admin form Ruby on Rails:Active Admin表单中的strftime() - Ruby on Rails: strftime() in Active Admin form Ruby On Rails Active Admin has_many更改下拉列表以使用不同的列 - Ruby On Rails Active Admin has_many changing dropdown to use a different column Ruby On Rails Active Admin has_many下拉菜单添加新对象 - Ruby On Rails Active Admin has_many dropdown to add new object 如何在Rails表单中获取所选下拉项的索引 - How to get the index of a selected dropdown item in a Rails form 如何使用Rails在数据库中存储下拉列表和其他选项文本输入数据? - How to store the dropdown, other option text input data in database using ruby on rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM