Rails 3: How to create a new nested resource?

后端 未结 3 821
日久生厌
日久生厌 2020-12-04 06:40

The Getting Started Rails Guide kind of glosses over this part since it doesn\'t implement the \"new\" action of the Comments controller. In my application, I have a book mo

3条回答
  •  暖寄归人
    2020-12-04 07:20

    Try @chapter = @book.build_chapter. When you call @book.chapter, it's nil. You can't do nil.new.

    EDIT: I just realized that book most likely has_many chapters... the above is for has_one. You should use @chapter = @book.chapters.build. The chapters "empty array" is actually a special object that responds to build for adding new associations.

提交回复
热议问题