dimanche 3 juillet 2016

Ruby on rails and composition in OOP: How can I access attributes in simple_form?

I am making a form for sign up and I have a class named ManagedCompany, that have an attribute named company of the class Company. The class Company have an attribute from another class named Address. So, there are compositions in those classes.

The classes are presented below:

app/models/managed_company.rb

class ManagedCompany
  include ActiveModel::Model

  def initialize
    @company = Company.new
  end

  attr_accessor :name, :string
  attr_accessor :company, :Company

end

app/models/company.rb

class Company
  include ActiveModel::Model

  def initialize
    @address = Address.new
  end

  attr_accessor :category, :string
  attr_accessor :name, :string
  attr_accessor :address, :Address

  validates_presence_of :category
  validates_presence_of :name

end

app/models/address.rb

class Endereco
  include ActiveModel::Model

  attr_accessor :street, :string
  attr_accessor :number, :string

  validates_presence_of :street
  validates_presence_of :number

end

My form:

<%= simple_form_for @managed_company do |form| %>

  <%= form.input :company.address.street, autofocus: true %>

<% end %>

But this way did not work. The error message:

undefined method `address' for :company:Symbol

So, in the form (simple_form of ruby on rails), how can I access the attribute street from the address object?

Aucun commentaire:

Enregistrer un commentaire