Search code examples
ruby-on-railsrubynested-attributes

Triple nested model not building correctly


I have an Organisme that has many Servicepoints and each Servicepoint has many Addresses. The Servicepoint is set to accepts_nested_attributes_for :addresses so that I can create a service point and at the same time create an Address with it.

My problem is that that, I can't figure out how to create the address that will be linked to the Servicepoint and that Servicepoint be linked to the Organisme. I am creating the Servicepoint inside the show view of an Organisme.

View:

<%= form_for([@organisme, @organisme.servicepoints.build]) do |f| %>
  <p>
    <%= f.label :nom %><br>
    <%= f.text_field :nom %>
  </p>
  <p>
    <%= f.label :fax %><br>
    <%= f.text_field :fax %>
  </p>
  <p>
    <%= f.label :courriel %><br>
    <%= f.email_field :courriel %>
  </p>
  <p>
    <%= f.label :telephone %><br>
    <%= f.text_field :telephone %>
  </p>
  <%= f.fields_for :addresses do |address_attributes| %>
    <p>
      <%= address_attributes.label :no_civique %><br>
      <%= address_attributes.text_field :no_civique %><br>

      <%= address_attributes.label :rue %><br>
      <%= address_attributes.text_field :rue %><br>

      <%= address_attributes.label :ville %><br>
      <%= address_attributes.text_field :ville %><br>

      <%= address_attributes.label :province %><br>
      <%= address_attributes.text_field :province %><br>

      <%= address_attributes.label :etat %><br>
      <%= address_attributes.text_field :etat %><br>

      <%= address_attributes.label :code_postal %><br>
      <%= address_attributes.text_field :code_postal %><br>
    </p>
<% end %>
  <p>
    <%= f.submit :Ajouter, class: 'btn btn-info' %>
  </p>
<% end %>

Controller:

organisme/show

def show
 @organisme = Organisme.find(params[:id])
end

servicepoint/create

def create

  @organisme = Organisme.find(params[:organisme_id])

  @servicepoint = @organisme.servicepoints.create(servicepoint_params)

  redirect_to organisme_path(@organisme)  

end

private
  def servicepoint_params
    params.require(:servicepoint).permit(:nom, :fax, :courriel, :telephone, addresses_attributes: [:id, :no_civique, :rue, :ville, :province, :etat, :code_postal])
end

Routes:

resources :organismes do
  member { patch :activate }
  member { patch :deactivate }
  resources :addresses
  resources :servicepoints do
    resources :addresses
  end
end

My problem right now is that the Address input information isn't even showing. I tried having a @servicepoint variable and then creating everything with that but the problem with that was that I could not link it to the Organisme.

If you need any more information I'll be happy to add anything.

Models:

class Organisme < ApplicationRecord
   has_many :addresses
   accepts_nested_attributes_for :addresses
   has_many :servicepoints
end

class Servicepoint < ApplicationRecord
   has_many :addresses
   accepts_nested_attributes_for :addresses
   belongs_to :organisme
end

class Address < ApplicationRecord
  belongs_to :organismereferent, optional: true
  belongs_to :organisme, optional: true
  belongs_to :servicepoint, optional: true
end

Solution

  • I would build everything in the controller action:

    <%= form_for([@organisme, @servicepoint]) do |f| %>
    ...
    
    
    def show
      @organisme = Organisme.find(params[:organisme_id])
      @servicepoint = @organisme.servicepoints.build
      @servicepoint.addressess.build
    end