【问题标题】:Trouble with form view for multil Nested Attributes多个嵌套属性的表单视图出现问题
【发布时间】:2016-04-28 02:35:22
【问题描述】:

我有具有多对多关系的 Book 模型和 Category 模型,并且 Book_Category 模型包含它们的 id。 我使用accepts_nested_attributes_for :book_categories 创建新书,然后更新 Book_Category 表。 我的参数是params.require(:book).permit(:name, :description, :author, book_categories_attributes:[]) .我只是先在 rails c 中进行测试,然后它就可以工作了。 Book.create!(name: 'asdsadsa',description: 'asdasdasdsadasdsadsad',author: 'asdsadasdasd',book_categories_attributes: [{ category_id: 1},{ category_id: 2}]) 但我不知道如何创建视图以将数组哈希类别发送到控制器。

【问题讨论】:

  • 你可以使用茧宝石。它在自述文件中有一些很好的例子。

标签: ruby-on-rails nested-attributes fields-for


【解决方案1】:

Rails 有一个很棒的宝石来处理嵌套属性https://github.com/ryanb/nested_form

另外,Ryan Bates(gem 作者)制作了一个示例应用程序https://github.com/ryanb/complex-form-examples/tree/nested_form

最后这里是一个相关的 RailsCast 插曲http://railscasts.com/episodes/196-nested-model-form-part-1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多