initialize data in edit using rails form object

328 views Asked by At

I cant initialize the data for class attributes with the use virtus gem. Here's my example code:

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

My haml form

= 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 ....

Data for shipment_detail fields(po_number, job_no) are not displaying inside the text field

0

There are 0 answers