モデルに別の属性がネストされています。
私の質問は、コントローラーまたはモデルでビルドを呼び出すためのより良い解決策は何ですか?
そのモデルを参照してください:
class ContentType < ActiveRecord::Base
after_initialize :add_fields
belongs_to :project
has_many :field_content_types
accepts_nested_attributes_for :field_content_types, reject_if: proc {|attributes| attributes['name'].blank?}
private
def add_fields
self.field_content_types.build if new_record?
end
end
モデルで削除after_initialize
し、コントローラーで行を追加します
class ContentTypesController < ApplicationController
def new
@content_type = ContentType.new
@content_type.field_content_types.build
end
end
ビルドインコントローラーを設定する理由は 1 つありますか?