简体   繁体   English

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

[英]Rails 3 and ActiveAdmin: get the value of an associated record on the form

Ok, I think this is going to be tricky on ActiveAdmin. 好的,我认为这在ActiveAdmin上会很棘手。

I need to issue a pop-up warning (I'll probably use jQuery) if there is an invoice created for a certain customer. 如果有为特定客户创建的发票,我需要发出弹出警告(我可能会使用jQuery)。 This would be easy if the invoice form had a select menu for the customers, but that's not my setup. 如果发票表单为客户提供选择菜单,这将很容易,但这不是我的设置。 Instead, this is the way it works: 相反,这是它的工作方式:

Shipment has_one Invoice
Shipment belongs_to Customer
(the New Shipment form has a select menu for customer)

Invoice belongs_to Shipment

So in my New Invoice form, I have a select menu for shipments. 因此,在我的新发票表格中,我有一个货运选择菜单。 To find out what customer the invoices belongs to, if i contains an instance of invoice I do i.shipment.customer . 要找出发票属于哪个客户,如果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

In the New Invoice form: how can I get the customer the shipment belongs to from the shipments select menu. 在“新发票”表单中:如何从“货件选择”菜单中获得货件所属的客户。

For example, user selects Shipment #1231. 例如,用户选择“货件#1231”。 If shipment belongs to customer_id 5, display jQuery alert. 如果装运属于customer_id 5,则显示jQuery警报。

(I already know how to include a javascript file in ActiveAdmin: Active Admin: Including Javascript ) (我已经知道如何在ActiveAdmin中包括javascript文件: Active Admin:包括Javascript

What you can do: make the collection for the shipment select list display the customer the 'current' invoice is connected to. 您可以执行的操作:使托运选择清单的集合显示“当前”发票所连接的客户。 So for the collection you could join with the Customer model, like so: 因此,对于集合,您可以加入“客户”模型,如下所示:

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] }

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM