三重嵌套模型无法正确构建

时间:2017-07-04 15:16:54

标签: ruby-on-rails ruby nested-attributes

我的OrganismeServicepoints个,ServicepointAddressesServicepointaccepts_nested_attributes_for :addresses设置为Servicepoint,以便我可以创建服务点,同时使用它创建地址。

我的问题是,我无法弄清楚如何创建与Servicepoint相关联的地址以及OrganismeServicepoint相关联的地址。我正在show的{​​{1}}视图中创建Organisme

查看:

<%= 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 /显示

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

服务点/创建

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变量,然后使用该变量创建所有内容,但问题是我无法将其链接到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

2 个答案:

答案 0 :(得分:1)

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

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


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

答案 1 :(得分:0)

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

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