好的,我认为这对 ActiveAdmin 来说会很棘手。
如果为特定客户创建了发票,我需要发出弹出式警告(我可能会使用 jQuery)。如果发票表格有一个供客户选择的菜单,这会很容易,但这不是我的设置。相反,这是它的工作方式:
Shipment has_one Invoice
Shipment belongs_to Customer
(the New Shipment form has a select menu for customer)
Invoice belongs_to Shipment
所以在我的新发票表格中,我有一个发货选择菜单。要找出发票属于哪个客户,如果 i
包含 invoice
的实例,我会执行 i.shipment.customer
。
# this is a snippet of my New Invoice form
form do |f|
f.inputs "Shipment Details" do
f.input :shipment_id, :label => "Shipment", :as => :select, :collection => Shipment.find(:all, :order => "file_number", :select => "id, file_number").map{|v| [v.file_number, v.id] }
f.input :issued_at, :label => "Date", :as => :datepicker
f.input :accounting_month, :label => "Accounting month", :as => :datepicker
end
在新发票表格中:如何从货件选择菜单中获取货件所属的客户。
例如,用户选择 Shipment #1231。如果 cargo 属于 customer_id 5,显示 jQuery 警报。
(我已经知道如何在 ActiveAdmin 中包含一个 javascript 文件:Active Admin: Including Javascript)
最佳答案
您可以做什么:使货件选择列表的集合显示“当前”发票所关联的客户。因此,对于集合,您可以加入 Customer 模型,如下所示:
f.input :shipment_id, :label => "Shipment", :as => :select,
:collection =>
Shipment.
joins(:customer).
order('shipments.file_number').
select("shipments.id, shipments.file_number || ' ' || customers.your_beautiful_customer_attribute as concatted").
map{|v| [v.concatted, v.id] }
希望对您有所帮助。
关于ruby-on-rails - Rails 3 和 ActiveAdmin : get the value of an associated record on the form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11298618/