【问题标题】:How to create a form for a has_many through relationship?如何通过关系为 has_many 创建表单?
【发布时间】:2015-09-21 20:26:31
【问题描述】:

在我的应用程序中,我有一个图书模型的图书索引页。把它想象成一个图书馆。

现在我有由列表组成的董事会。假设我有一个名为 Categories 的板。

所以我去了分类委员会。在我的网址中是 www.example.com/board/1

在这个类别板上有很多列表。假设我有一个名为编程书籍的列表。

所以现在我需要将书籍添加到这个名为 Programming Books 的列表中。

我已经完成了所有设置,只是不知道如何将书籍添加到列表中。当我单击添加书籍时,我如何获得该 list_id?我希望能够单击添加书籍,然后转到列出所有书籍的页面。然后我想通过检查每一本书来选择书籍,然后将它们添加到列表中。

不用担心创建列表或看板我已经正确设置了我只是想将书籍添加到列表中。

类别板 (www.example.com/board/1)

===================     ===================     
=Programming Books=     =Adventure Books  =
===================     ===================
= Book 1          =     = add books       =
= Book 2          =     =                 =
= Book 3          =     =                 =
= add books       =     =                 =
===================     ===================

列表模型

belongs_to :user, inverse_of: :list

has_many :list_books
has_many :books, through: :list_books

accepts_nested_attributes_for :list_books, :allow_destroy => true

List_Books 模型

belongs_to :list
belongs_to :books

图书模型

belongs_to :user, inverse_of: :books

has_many :list_books
has_many :lists, through: :list_books

accepts_nested_attributes_for :list_books, :allow_destroy => true

列表控制器

def addbooks
  // Not sure what to put here? It needs to grab the list_id from the list where i clicked add books.
end

private
  def_params
    // Not sure what params i need
  end

添加图书视图

// i need of list of all the books here. Then i want to check each book i want and then submit.

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 has-many-through has-many


    【解决方案1】:

    您不需要在单独的操作中处理它,只需常规的创建操作并添加accept_nest_attributes,如本例所示

    class Book < ActiveRecord::Base 
        has_many :classifications, :dependent => :destroy, :autosave => true , :inverse_of => :book accepts_nested_attributes_for :classifications, :allow_destroy => true, :reject_if => :all_blank 
        has_many :categories, :through => :classifications 
    end
    
    
    class Category < ActiveRecord::Base 
       has_many :classifications, :dependent => :destroy, :autosave => true , :inverse_of => :category accepts_nested_attributes_for :classifications, :allow_destroy => true, :reject_if => :all_blank 
      has_many :books, :through => :classifications 
    end
    
    
    class Classification < ActiveRecord::Base 
      belongs_to :category, :inverse_of => :classifications 
      belongs_to :book, :inverse_of => :classifications 
    end
    

    然后将其添加到您的视图中。

    <%= form_for @book do |f| %>
    
     <p>
     <%= f.label :name %>
     <%= f.text_field :name %>
     </p>
    
     <p>Categories</p>
     <ul>
     <% @categories.each do |cat| %>
     <%= hidden_field_tag "book_category_ids_none", nil, {:name => "book[category_ids][]"}%>
     <li>
     <%= check_box_tag "book_category_ids_#{cat.id}", cat.id, (f.object.categories.present? && f.object.categories.include?(cat.id)), {:name => "book[category_ids][]"} %>
     <%= label_tag "book_category_ids_#{cat.id}", cat.name %>
     </li>
     <% end %>
     </ul>
    
     <%= f.submit %>
    
    <% end %>
    

    查看完整示例here

    【讨论】:

    • 这实际上行不通,你看我有很多由列表组成的板。就像一个板可能被称为具有子类型列表的动作书。然后另一个板可能被称为具有子类型列表的教育书籍。我需要获取 list_id 并以这种方式添加书籍。有点像音乐库和播放列表,您如何将库中的歌曲添加到播放列表。这些列表就像播放列表,书籍就是图书馆。
    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多