1

我无法使用使用 virtus gem 初始化类属性的数据。这是我的示例代码:

class ShipmentForm
  include Virtus.mode

  attr_reader :shipment
  attr_reader :shipment_detail

  attribute :shipment_type, String
  attribute  :shipment_detail, ShipmentDetailAttribute

  def initialize(attr = {})
   if !attr['id'].nil?
    @shipment = Shipment.find(attr['id'])
    @shipment_detail = @shipment.shipment_detail
    self[:shipment_type] = attr[:shipment_type].nil? ? @shipment.shipment_type : attr[:shipment_type]
     self[:shipment_detail] = attr[:shipment_detail].nil? ? @shipment_detail.attributes : attr[:shipment_detail]
   else
    super(attr)
   end
  end
end

class ShipmentDetailAttribute
  include Virtus.model

  attribute :po_number
  attribute :job_no
  etc...
end

我的haml表格

= form_for @shipment, url: shipments_path, method: :patch do |f|
  = f.text_field :shipment_type
  = f.fields_for :shipment_detail do |detail|
    = detail.text_field :po_number
    = detail.text_field :job_no
    etc ....

ship_detail 字段(po_number,job_no)的数据未显示在文本字段中

4

0 回答 0