ruby-on-rails - Rails 3 和 ActiveAdmin : get the value of an associated record on the form

标签 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。如果 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/

相关文章:

python - Ruby 有类似 bpython 的东西吗?

ruby-on-rails - Where 子句按 "ANY"过滤 - 这是什么意思?

ruby-on-rails - 没有路由匹配 GET/sign_up,但我可以访问该页面?

ruby-on-rails - Rmagick已安装: `require' : no such file to load -- RMagick (LoadError)

ruby-on-rails - Rails 3 和 ActiveAdmin。如何添加虚拟模型?

ruby-on-rails-3 - 在两个事件的管理类中使用相同的模型

ruby-on-rails - 事件管理员导出到 CSV 文件

ruby-on-rails - 在Rails引擎 View 中缺少设计路线助手

sql - 用排序重构 Ruby on Rails link_to

ruby-on-rails - 如何让服务器端 faye 客户端在我的 Rails 应用程序中运行?