三重嵌套模型未正确构建

Triple nested model not building correctly

我有一个 Organisme 有很多 Servicepoints,每个 Servicepoint 有很多 AddressesServicepoint 设置为 accepts_nested_attributes_for :addresses 这样我就可以创建一个服务点,同时用它创建一个地址。

我的问题是,我不知道如何创建将 linked 到 Servicepoint 的地址,并且 Servicepoint 是 link 到 Organisme。我正在 Organisme.

show 视图中创建 Servicepoint

查看:

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

控制器:

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

路线:

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

我现在的问题是 Address 输入信息甚至没有显示。我尝试使用 @servicepoint 变量,然后用它创建所有内容,但问题是我无法 link 它到 Organisme.

如果您需要更多信息,我很乐意添加任何内容。

型号:

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

在表单中构建了一个服务点,因此一个空的服务表单显示为一个空表单。没有显示地址,因为没有要迭代的地址。你也需要建立地址。

请注意,由于表单本身的构建,您的表单永远不允许您编辑服务点。为了让表单显示多个服务点,您需要在某处迭代服务点。

我会在控制器操作中构建所有内容:

<%= form_for([@organisme, @servicepoint]) do |f| %>
...


def show
  @organisme = Organisme.find(params[:organisme_id])
  @servicepoint = @organisme.servicepoints.build
  @servicepoint.addressess.build
end