簡體   English   中英

Rails 3和ActiveAdmin:獲取表單上關聯記錄的值

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

好的,我認為這在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

在“新發票”表單中:如何從“貨件選擇”菜單中獲得貨件所屬的客戶。

例如,用戶選擇“貨件#1231”。 如果裝運屬於customer_id 5,則顯示jQuery警報。

(我已經知道如何在ActiveAdmin中包括javascript文件: Active Admin:包括Javascript

您可以執行的操作:使托運選擇清單的集合顯示“當前”發票所連接的客戶。 因此,對於集合,您可以加入“客戶”模型,如下所示:

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

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM