4

スタックオーバーフローとグーグルで検索しましたが、役に立ちませんでした。

近親者が 1 人います

入れ子になった形(繭あり)の人を作ることができて、完全に保存されます。何らかの理由で編集ページに移動すると、関連する next_of_kin レコードが削除されます。レコードのデータが入力されたフィールドをレンダリングしますが、データベース内の実際のレコードは削除されます。

私のフォーム

.full-width-row
  = simple_form_for @person, url: {action: action}, wrapper: 'two_column_form' do |f|
    .columns.medium-4
      h4 = heading
    .columns.medium-8
      = f.button :submit, 'Save', class: 'right button tiny radius'

    .columns.medium-12
      .row
        .medium-8.columns
          = f.input :first_name
          = f.input :last_name
          = f.input :email
          br

          h6 Next of Kin
          br
          = f.simple_fields_for :next_of_kin do |nok|
            = render 'next_of_kin_fields', f: nok
          .link
            = link_to_add_association "Add Next of Kin", f, :next_of_kin, class: 'button secondary tiny next_of_kin_button'
          hr

私の _next_of_kin_fields パーシャル

.nested-fields
  = f.input :first_name
  = f.input :last_name
  = f.input :relationship, as: :select, collection: NextOfKin::RELATIONSHIP
  = f.input :telephone
  = link_to_remove_association "Remove next of kin", f, class: 'remove-association button tiny alert'

私の人物モデル:

class Person < ActiveRecord::Base
  has_one :next_of_kin, dependent: :destroy
  accepts_nested_attributes_for :next_of_kin, allow_destroy: true
end

私の近親者モデル:

class NextOfKin < ActiveRecord::Base
  belongs_to :person
  RELATIONSHIP = [ "Mother", "Father", "Brother", "Sister", "Aunt", "Uncle", "Spouse", "Other"]
end

編集ページにアクセスしたときに next_of_kin レコードが削除されないようにするにはどうすればよいですか?

4

1 に答える 1

4

これを回避するためにに設定force_non_association_createしますlink_to_add_associationtrue

= link_to_add_association "Add Next of Kin", f, :next_of_kin, force_non_association_create: true, class: 'button secondary tiny next_of_kin_button'

このパラメーターに関する Cocoon のドキュメント: https://github.com/nathanvda/cocoon#force_non_association_create

于 2015-01-28T20:15:03.927 に答える