Ruby on rails Rails集合\u选择多对多不在联接表中创建条目
我正在开发一个类似于图书馆预订的应用程序。我一直在为添加新书而构建嵌套表单。我得到了一对多关系(一本书的图像等)的表单,但我还想选择一个现有作者,并将其与该书(一本书有许多作者,一个作者通过贡献表有许多书)绑定在一起 提交表单时,它不会在联接表中使用book_id和author_id创建条目。(在创建新作者时,我设法让它使用简单的文本输入字段,但集合\u select不起作用。在将reject\u if添加到accepts\u嵌套的\u属性之前,它一直使用空的first\u name和last\u name创建一个新作者,并使用此空的新作者在联接表中创建一个条目) 这是我的表格,只剩下重要的部分Ruby on rails Rails集合\u选择多对多不在联接表中创建条目,ruby-on-rails,ruby-on-rails-4,nested-forms,fields-for,Ruby On Rails,Ruby On Rails 4,Nested Forms,Fields For,我正在开发一个类似于图书馆预订的应用程序。我一直在为添加新书而构建嵌套表单。我得到了一对多关系(一本书的图像等)的表单,但我还想选择一个现有作者,并将其与该书(一本书有许多作者,一个作者通过贡献表有许多书)绑定在一起 提交表单时,它不会在联接表中使用book_id和author_id创建条目。(在创建新作者时,我设法让它使用简单的文本输入字段,但集合\u select不起作用。在将reject\u if添加到accepts\u嵌套的\u属性之前,它一直使用空的first\u name和last\
<%= 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 %>
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
我的书
私人的
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
你能展示你在多对多关系中使用的所有三个模型吗
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
class医生
关系定义本身应该有一些问题,这通常是直接起作用的如果我理解正确,您应该利用您的
加入表贡献
。将部分的字段更改为这将使其起作用
<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>
添加作者
从现有作者中选择
这行
应该是
吗?谢谢你的评论!如果我尝试这一点,我会在书中看到一个命名错误#new。未定义的方法“merge”表示:full\u name:Symbol我不太确定这是什么意思(我在上面的作者模型中定义了full\u name)。再次感谢!您可以在提交表单后发布日志信息吗?
?抱歉,我忘了删除您建议行中的:id。我尝试了您所说的内容,现在我为#获得了一个未定义的方法“author_id”。可能您应该将这行
更改为
,然后尝试我的第一个建议。谢谢,我已经添加了代码的贡献模型,请看一看。确切地说,我以前尝试过这一点,但没有成功,因为这一行已经存在,花了我整整两天的时间:验证:book_id,presence:true
按照Pavan在回答中的建议执行,并删除我的书模型中的行使其成功。谢谢!
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
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
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
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
<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>