Rails 3和ActiveAdmin:获取表单上相关记录的值

时间:2012-07-02 17:49:58

标签: ruby-on-rails ruby activeadmin formtastic

好的,我认为这对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。如果货件属于customer_id 5,则显示jQuery警报。

(我已经知道如何在ActiveAdmin中包含javascript文件:Active Admin: Including Javascript

1 个答案:

答案 0 :(得分:0)

您可以做什么:使货件选择清单的集合显示“当前”发票所连接的客户。因此,对于集合,您可以加入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] }

希望它有所帮助。