嵌套表单总是在更新后插入新行

Nested form is always inserting a new row after update

我需要将我的 parties 模型关联到两种地址:main_addressbilling_address

我正在使用 has_one/belongs_to 协会。 创建新派对一切正常。

但每次我更新任何一方时,都会在地址 table 上创建一个新行,而不是只更新当前关联的地址。我想我错过了什么。

这是我现在拥有的:

派对模特

class Party < ActiveRecord::Base
  has_one :main_address, :class_name => "Address"
  has_one :billing_address, :class_name => "Address"

  accepts_nested_attributes_for :main_address, :allow_destroy => true
  accepts_nested_attributes_for :billing_address, :allow_destroy => true
end

派对数据库

create_table "parties", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at",                        null: false
    t.datetime "updated_at",                        null: false
    t.string   "type"
    t.hstore   "additional_information"
    t.string   "cpf_or_cnpj",            limit: 15
    t.integer  "main_address_id"
    t.integer  "billing_address_id"
  end

地址模型

class Address < ActiveRecord::Base
  belongs_to :party
end

地址数据库

  create_table "addresses", force: :cascade do |t|
    t.string   "line1"
    t.string   "line2"
    t.string   "city"
    t.string   "state"
    t.string   "zip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "party_id"
  end

在我的派对视图中,我使用:

<%= simple_form_for @party, :validate => true  do |f| %>
   <div id="main_address">
     <%= f.simple_fields_for :main_address do |main_address| %>
        <%= render "address", :f => main_address %>
      <% end %>
    </div>
<% end %>

部分地址:

<div class="nested-fields form-inline">
  <%= f.input :zip %>
  <%= f.input :line1 %>
  <%= f.input :line2 %>
  <%= f.input :city %>
  <%= f.input :state %>
</div>

各方控制者

class PartiesController < ApplicationController

  before_action :set_party, only: [:show, :edit, :update, :destroy]
  before_action :set_type

  def show
  end

  def index
    @parties = type_class.all
  end

  def new
    @party = type_class.new
  end

  def edit
     @party.build_main_address unless @party.main_address.present?
  end

  def create 
      @party = type_class.new(party_params)
      if @party.save
          redirect_to parties_url, notice: (t "#{type}") +" successfully created."
        else
          render action: 'new'
      end
  end

  def update
    if @party.update(party_params )
      redirect_to @party, notice: "#{type} was successfully created."
    else
      render action: 'edit'
    end
  end

private

  def set_type 
     @type = type
  end

  def type 
      Party.types.include?(params[:type]) ? params[:type] : "Party"
  end

  def type_class 
      type.constantize 
  end

  def set_party
    @party = type_class.find(params[:id])
  end

  def party_params   
      params.require(type.underscore.to_sym).permit(:name, :type,:cpf_or_cnpj,main_address_attributes:[:zip,:line1,:line2,:state,:city,:type], contact_informations_attributes: [:id, :_destroy, :type, :label, :info])
  end

end

:id 添加到 party_params 中的 main_address_attributes 以使 更新 正常工作。

def party_params   
  params.require(type.underscore.to_sym).permit(:name, :type,:cpf_or_cnpj,main_address_attributes:[:id, :zip,:line1,:line2,:state,:city,:type], contact_informations_attributes: [:id, :_destroy, :type, :label, :info])
end