Rails 4:在创建模型时能够选择一个模型的多个实例,它具有has_many:through关系,与

jocab_w

当我向其中添加新项BookLibrary,我希望能够指定什么Shelves注意:Shelf”是我在应用程序中用于模型的术语,该模型Books在一个分组Library;该模型的更好名称应该是CategoryGenre,但是我很愚蠢),因为Library它应该属于。现在,我能够将添加Book到我的Library,但我在使用的问题Shelves我是学习Rails的新手,我将不胜感激。

楷模

user.rb:

class User < ActiveRecord::Base
  has_one :library, dependent: :destroy
  ...
end

library.rb:

class Library < ActiveRecord::Base
  belongs_to :user
  has_many :shelves, dependent: :destroy

  has_many :catalogs, dependent: :destroy
  has_many :books, :through => :catalogs, dependent: :destroy
  ...
end

book.rb:

class Book < ActiveRecord::Base
  has_many :catalogs, dependent: :destroy
  has_many :libraries, :through => :catalogs, dependent: :destroy

  has_many :bookshelves, dependent: :destroy
  has_many :shelves, :through => :bookshelves, dependent: :destroy
  ...
end

catalog.rb:

class Catalog < ActiveRecord::Base
  belongs_to :book
  belongs_to :library
end

rack.rb:

class Shelf < ActiveRecord::Base
  belongs_to :library
  has_many :bookshelves, dependent: :destroy
  has_many :books, :through => :bookshelves, dependent: :destroy
  ...
end

bookshelf.rb

class Bookshelf < ActiveRecord::Base
  belongs_to :book
  belongs_to :shelf
end

控制器

books_controller.rb

class BooksController < ApplicationController
  ...

  def create
    @book = Book.new(book_params)
    @library = current_user.library

    if @book.save
      @book.catalogs.create(:library_id => @library.id)
      flash[:success] = "Book added to library!"
      redirect_to current_user
    else
      render 'current_user'
    end
  end

  ...

  private
    def book_params
      params.require(:book).permit(:title, :author, :publisher, :isbn)
    end

    ...
end

看法

_book_form.html.erb:

<%= form_for(@book) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :title, placeholder: "Book Title" %>
    <%= f.text_field :author, placeholder: "Author" %>
    <%= f.text_field :publisher, placeholder: "Publisher" %>
    <%= f.text_field :isbn, placeholder: "ISBN" %>

    <%= f.fields_for :catalogs do |ff| %>
      <%= ff.hidden_field :library_id %>
    <% end %>

    <%= f.fields_for :bookshelves do |ff| %>
      <%= ff.collection_select :shelf_ids, current_user.library.shelves.all.collect {|x| [x.name, x.id]}, {}, multiple: true %>
    <% end %>
  </div>
  <%= f.submit "Add Book to Library", class: "btn btn-primary" %>
<% end %>
瑞安·K

在您的book_params方法中,您没有shelf_ids列出。因此,它将不会传递到.new方法中,因此不会被保存。由于您ids不需要多个,所以您想这样做:

def book_params
  params.require(:book).permit(:title, :author, :publisher, :isbn, shelf_ids: [])
end

这样,控制器就知道它是一个数组,而不仅仅是一个值。

哦,顺便说一句,shelf_ids其他类似的数组值必须在末尾,否则将引发错误。

编辑:

还要更改这部分代码:

<%= f.fields_for :bookshelves do |ff| %>
  <%= ff.collection_select :shelf_ids, current_user.library.shelves.all.collect {|x| [x.name, x.id]}, {}, multiple: true %>
<% end %>

对此:

<%= f.collection_select :shelf_ids, current_user.library.shelves.all.collect {|x| [x.name, x.id]}, {}, multiple: true %>

您无需担心through关联一部分。只需设置has_many零件即可使用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails,一个模型有多个has_many

来自分类Dev

Rails:从2个模型中选择具有Unique_to和has_many关系的唯一值

来自分类Dev

在与另一个模型有 has_many 关系的模型上应用 NOT IN like 条件,Rails

来自分类Dev

Rails 4 has_many:through关系:在从父模型创建动作时分配默认值以加入模型属性

来自分类Dev

rails 相同模型之间的多个 has_many 关系

来自分类Dev

Rails模型关系has_many所有

来自分类Dev

Rails-从另一个模型中创建模型的实例

来自分类Dev

Rails 4+ has_many通过一个有关系吗?

来自分类Dev

如何在Rails中的同一模型(用户)之间建立has_many:through关系?

来自分类Dev

Rails查询具有has_many和belongs_to关系的模型

来自分类Dev

在同一模型中的Rails关联“ has_many:through”

来自分类Dev

Rails 4的嵌套属性不会创建has_many模型

来自分类Dev

has_many通过关系可以在Rails中使用4个模型吗?

来自分类Dev

Rails:通过has_many关联的最后一个元素对模型进行排序

来自分类Dev

Rails:同时创建模型和联接表,has_many通过

来自分类Dev

Rails:如何创建具有两个“ belongs_to”关系的模型,其中一个始终为空?

来自分类Dev

在Rails中动态创建模型的多个实例

来自分类Dev

Rails模型多个has_many:通过关系输出到json

来自分类Dev

Ajax调用以创建模型onClick的实例(Rails 4)

来自分类Dev

Rails关联has_many通过不使用多个模型

来自分类Dev

Rails在has_many:through关系中创建数据

来自分类Dev

rails 4 has_many对于相同模型的不同实例的工作方式不同

来自分类Dev

rails模型has_many本身

来自分类Dev

Rails 4中has_many关系的多个条件

来自分类Dev

Rails has_many但在关联模型的列上是唯一的

来自分类Dev

在Rails中选择has_many关系之一

来自分类Dev

带有 has_many、through、class_name 和 where 子句的 Rails 模型关联

来自分类Dev

在Active Record(Rails)中,如何从has_many关系中为每个父记录选择一个列的总和?

来自分类Dev

Rails计算具有has_many关系的行

Related 相关文章

  1. 1

    Rails,一个模型有多个has_many

  2. 2

    Rails:从2个模型中选择具有Unique_to和has_many关系的唯一值

  3. 3

    在与另一个模型有 has_many 关系的模型上应用 NOT IN like 条件,Rails

  4. 4

    Rails 4 has_many:through关系:在从父模型创建动作时分配默认值以加入模型属性

  5. 5

    rails 相同模型之间的多个 has_many 关系

  6. 6

    Rails模型关系has_many所有

  7. 7

    Rails-从另一个模型中创建模型的实例

  8. 8

    Rails 4+ has_many通过一个有关系吗?

  9. 9

    如何在Rails中的同一模型(用户)之间建立has_many:through关系?

  10. 10

    Rails查询具有has_many和belongs_to关系的模型

  11. 11

    在同一模型中的Rails关联“ has_many:through”

  12. 12

    Rails 4的嵌套属性不会创建has_many模型

  13. 13

    has_many通过关系可以在Rails中使用4个模型吗?

  14. 14

    Rails:通过has_many关联的最后一个元素对模型进行排序

  15. 15

    Rails:同时创建模型和联接表,has_many通过

  16. 16

    Rails:如何创建具有两个“ belongs_to”关系的模型,其中一个始终为空?

  17. 17

    在Rails中动态创建模型的多个实例

  18. 18

    Rails模型多个has_many:通过关系输出到json

  19. 19

    Ajax调用以创建模型onClick的实例(Rails 4)

  20. 20

    Rails关联has_many通过不使用多个模型

  21. 21

    Rails在has_many:through关系中创建数据

  22. 22

    rails 4 has_many对于相同模型的不同实例的工作方式不同

  23. 23

    rails模型has_many本身

  24. 24

    Rails 4中has_many关系的多个条件

  25. 25

    Rails has_many但在关联模型的列上是唯一的

  26. 26

    在Rails中选择has_many关系之一

  27. 27

    带有 has_many、through、class_name 和 where 子句的 Rails 模型关联

  28. 28

    在Active Record(Rails)中,如何从has_many关系中为每个父记录选择一个列的总和?

  29. 29

    Rails计算具有has_many关系的行

热门标签

归档