我是 ruby on rails 的新手,尤其是嵌套形式。我正在尝试以相同的形式创建一个作者和一本书,知道它们之间存在多对多的关系,我的代码如下所示。书本.rb
class Book < ActiveRecord::Base
has_many :exemplaires
has_many :talks, inverse_of: :book
has_many :subjects, through: :writings
has_many :writings
has_many :authors, through: :talks
accepts_nested_attributes_for :authors
validates :title, presence: true
end
作者.rb:
class Author < ActiveRecord::Base
has_many :talks
has_many :books, through: :talks
end
谈话.rb
class Talk < ActiveRecord::Base
belongs_to :book
belongs_to :author
end
book_controller.rb
class BooksController < ApplicationController
def index
end
def list
@books= Book.all
end
def new
@book = Book.new
@author=@book.authors.new
end
def create
@book= Book.new(book_params)
if @book.save
flash[:notice]='goood'
redirect_to admin_manage_path
else
flash[:alert]='ouups'
redirect_to root_url
end
end
private
def book_params
params.require(:book).permit(:title, :pages, :resume, :authors_attributes)
end
end
书籍\new.html.erb
<h1>Add new book</h1>
<%= form_for(@book) do |form| %>
<%= form.label :title %>
<%= form.text_field :title %>
<%= form.fields_for :authors do |tag_form| %>
<%= tag_form.label :f_name %>
<%= tag_form.text_field :f_name %>
<% end %>
<%= form.submit "Submit" %>
<% end %>
我得到的错误
BooksController#create 作为 HTML 参数处理:{"utf8"=>"✓", "authenticity_token"=>"qpGng8tOiC/B5VX2tphuhAe+Wq1vx7it1vEO6XmwZmI=", "book"=>{"title"=>"boooooooooooooook", "authors_attributes "=>{"0"=>{"f_name"=>"auuuuuuuuuuuuuuuuuthor"}}}, "commit"=>"Submit"} 不允许的参数:authors_attributes