多个嵌套 has_many 在 activeadmin

multiple nested has_many in activeadmin

我有以下course.rb模型

has_many :chapters
  has_many :lectures, through: :chapters
  has_many :enrols
  has_many :contents, through: :lectures
  has_many :users, -> { uniq }, through: :enrols
  accepts_nested_attributes_for :chapters, allow_destroy: true
  accepts_nested_attributes_for :lectures, allow_destroy: true

course.rb 活动管理文件

form title: 'Edit Course' do |f|
    f.inputs 'Details' do
      f.input :course_name
      f.input :course_subtitle
      f.input :course_description
      f.input :video_link
      f.input :course_language
      f.input :status
      f.input :course_image
    end

    # has_many :contents do |content|
    #   content.input :description
    #   content.input :attachment
    # end

    f.has_many :chapters, allow_destroy: true do |chapter|
      # chapter.input :title
      chapter.has_many :lectures do |lecture|
        # lecture.input :title
        # lecture.has_many :contents do |content|
        #   content.input :description
        # end
        lecture.input :title
      end
    end
    actions
  end

我正在尝试使 coursecontent 在课程表中可编辑,它需要多个嵌套 has_many,因为它有 has_many 个关系.

现在我得到 undefined methodnew_record?对于 nil:NilClass` 错误

怎么办?有更好的方法吗?

undefined method new_record?' for nil:NilClass`

根本原因:

chapter.has_many :lectures do |lecture|

原因:

你在 Course 模型中有 accepts_nested_attributes_for :lectures, allow_destroy: true,所以你 不能在 Chapter

中有嵌套讲座

解法:

chapter.has_many :lectures do |lecture|

应该是

f.has_many :lectures do |lecture|

如果您想在 chapter 中包含 嵌套讲座 ,请从 Course 模型中删除 accepts_nested_attributes_for :lectures, allow_destroy: true 并将其添加到 [=15] 中=] 模型.

What if i want to add only the lecture? Your suggestion seem to work but it gave me option to add lecutre and chapter

那样的话,就不需要f.has_many :chapters, allow_destroy: true do |chapter|了。只要 f.has_many :lectures do |lecture|