Rails嵌套属性给出错误:未定义的方法`build_priority'

问题描述 投票:0回答:1

整天但找不到简单的问题。为什么我的嵌套属性不能使用此关联?

Here is error: NoMethodError (undefined method `build_priority' for
#<Audit:0x134234jnjn2j>):   app/controllers/audits_controller.rb:43:in `new'

audits_controller.rb

 def new
    @audit = Audit.new
    @audit.build_priority 
 end

audit.rb模型

  has_many :priorities, dependent: :destroy
  accepts_nested_attributes_for :priorities, allow_destroy: true, reject_if: :all_blank

priority.rb模型

class Priority < ActiveRecord::Base
  belongs_to :audit
  accepts_nested_attributes_for :audit, allow_destroy: true, reject_if: :all_blank
end
ruby-on-rails ruby-on-rails-4 associations nested-forms has-many
1个回答
0
投票
 def new
    @audit = Audit.new
    @audit.priorities.build
 end

楷模:-

class Audit < ActiveRecord::Base
  has_many :priorities, dependent: :destroy
  accepts_nested_attributes_for :priorities, allow_destroy: true, reject_if: :all_blank

end

class Priority < ActiveRecord::Base
  belongs_to :audit
  #accepts_nested_attributes_for :audit, allow_destroy: true, reject_if: :all_blank
end
© www.soinside.com 2019 - 2024. All rights reserved.