Rails collection_select与多对多不在连接表中创建条目



我正在开发一个类似于图书馆预订应用程序的应用程序。我正在构建一个用于添加新书的嵌套表单。我得到了一对多的关系(图像为一本书等)的形式工作,但我也想选择和绑定一个现有的作者到那本书(一本书有许多作者和作者有许多书通过贡献表)与collection_select。

在提交表单时,它不会在带有book_id和author_id的连接表中创建条目。(在创建新作者时,我设法使它与简单的text_entry字段一起工作,但是collection_select不起作用。在将reject_if添加到accepts_nested_attributes之前,它一直使用空白的first_name和last_name创建一个新作者,并在连接表中使用这个空白的新作者创建一个条目)

这是我的表单中最重要的部分

    <%= form_for @book do |f| %>
    <div class="panel panel-default">
        <div class="panel-body">
            <%= f.label :name, "Book title" %>
            <%= f.text_field :name %>
            <%= f.label :description %>
            <%= f.text_area :description, rows: 5 %>
            <%= f.label :year_of_publication %>
            <%= f.text_field :year_of_publication %>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-body">
            <h3>Add authors</h3>
            <h4>Choose from existing authors</h4>
            <%= f.fields_for :authors do |builder| %>
                <%= builder.collection_select( :id, Author.all, :id, :full_name, prompt: "Select from existing authors") %>
            <% end %>
        </div>
    </div>
    <%= f.submit "Submit", class: "btn btn-info" %>
<% end %>

这是当前为collection_select

呈现的HTML
<select id="book_authors_attributes_0_id" name="book[authors_attributes][0][id]"><option value="">Select from existing authors</option>
<option value="1">Berman, Jules J.</option>
<option value="2">Writerton, Andy R.</option>
<option value="3">Goldner, Merle</option>
<option value="4">Auer, Cordell</option>
<option value="5">Metz, Dewitt</option>
<option value="6">Leffler, Briana</option>
<option value="7">Trantow, Audra</option>
<option value="8">Murazik, Ebony</option>
<option value="9">Bahringer, Cale</option>
<option value="10">Schmitt, Wiley</option>
<option value="11">Casper, Zoe</option>

这是我的Book模型。

class Book < ActiveRecord::Base
# default_scope -> { order('name ASC') }
validates :year_of_publication, presence: true
validates :description, presence: true
validates :name, presence: true
has_many :stock_items,      dependent: :destroy
has_many :libraries,        through: :stock_items
has_many :contributions,    dependent: :destroy 
has_many :authors,          through: :contributions
has_many :bookings,         through: :stock_items
has_many :book_images, dependent: :destroy
accepts_nested_attributes_for :book_images
accepts_nested_attributes_for :authors, :allow_destroy => true, :reject_if => proc {|attributes| attributes['last_name'].blank? }
accepts_nested_attributes_for :libraries
accepts_nested_attributes_for :stock_items
accepts_nested_attributes_for :contributions
validates :name, presence: true
# validate year of pub length to 4
end

和我的Author模型。

class Author < ActiveRecord::Base
validates :first_name, presence: true
validates :last_name, presence: true
has_many :contributions, dependent: :destroy
has_many :books, through: :contributions
def full_name
    "#{last_name}, #{first_name}"
end
end

贡献模型(连接表)

class Contribution < ActiveRecord::Base
    belongs_to :author
    belongs_to :book
    validates :author_id, presence: true
    validates :book_id, presence: true
end

模式的相关部分

  create_table "authors", force: true do |t|
    t.string   "last_name"
    t.string   "first_name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
  create_table "contributions", force: true do |t|
    t.integer  "book_id"
    t.integer  "author_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
  create_table "books", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "year_of_publication"
    t.string   "description"
  end

My new and create actions in BooksController

def new
@book = Book.new
# Displays empty fields in the new book action view (n.times)
1.times { @book.authors.build }
1.times { @book.book_images.build }
@book.stock_items.build
end
def create
@book = Book.new(book_params)
if @book.save
  redirect_to @book
  flash[:success] = "Book added"
else
  render 'new'
  flash.now[:danger] = "Book NOT added"
end
end
我book_params

私人

def book_params
  # Include the nested parameters in the strong parameters as model_name_attributes !!!!!!!!!!
  params.require(:book).permit( :name, 
                                :description, 
                                :year_of_publication, 
                                authors_attributes: [ :first_name, :last_name ], 
                                book_images_attributes: [ :image_url, :book_id ], 
                                libraries_attributes: [ :name ], 
                                stock_item_attributes: [ :book_id, :library_id ], 
                                contribution_attributes: [ :book_id, :author_id ])
end

如果我理解正确的话,您应该使用您的 join_table contributions 。将 fields_for 部分更改为此将使其工作

<div class="panel panel-default">
        <div class="panel-body">
            <h3>Add authors</h3>
            <h4>Choose from existing authors</h4>
            <%= f.fields_for :contributions do |builder| %>
            <%= builder.collection_select(:author_id, Author.all, :id, :full_name, prompt: "Select from existing authors") %>
            <% end %>
        </div>
    </div>

您能展示用于多对多关系的所有三个模型吗?

class Physician < ActiveRecord::Base
attr_accessible :name, :title
  has_many :appointments
  has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
 attr_accessible :details, :patient_id, :physician_id
  belongs_to :physician
  belongs_to :patient
end
class Patient < ActiveRecord::Base
  attr_accessible :name , :details
  has_many :appointments
  has_many :physicians, through: :appointments
end

关系定义本身应该有一些问题这通常是直接工作的

相关内容

  • 没有找到相关文章

最新更新