Ruby on rails 为的简单表单提供的d和帮助程序(当您使用的简单表单时)

Ruby on rails 为的简单表单提供的d和帮助程序(当您使用的简单表单时),ruby-on-rails,ruby,ruby-on-rails-4,nested-attributes,Ruby On Rails,Ruby,Ruby On Rails 4,Nested Attributes,步骤1 在您的书籍模型中,您应该为:author添加接受嵌套的属性 步骤2 修改books\u controller.rb的新方法 由于您在图书模型中拥有所属的作者,您的图书控制器的新方法将是 def new @book = Book.new @book.build_author #This is very important end 步骤3 您的book_params方法应修改为 def book_params params.require(:book).permit(:title, :sy

步骤1

在您的
书籍
模型中,您应该为:author添加
接受嵌套的属性

步骤2

修改books\u controller.rb的新方法

由于您在
图书
模型中拥有
所属的作者
,您的
图书控制器
新方法将是

def new
@book = Book.new
@book.build_author #This is very important
end
步骤3

您的
book_params
方法应修改为

def book_params
params.require(:book).permit(:title, :synopsis, :body, :jacket_cover,author_attributes: [:name,:biography,..,..])
end
步骤4

最后,您的
图书创建的
表单
如下所示

<%= simple_form_for(@book, :html => { :multipart => true }) do |f| %>
  <%= f.error_notification %>

  <div class="inputs">
    <%= f.file_field :jacket_cover %>
    <%= f.input :title %>
    <%= f.input :synopsis %>
    <%= f.input :body %>
  </div>

  <%= f.simple_fields_for :author do |a| %>

  ... author fields...
  ....................
  <% end %>

  <div class="actions">
    <%= f.button :submit %>
  </div>
<% end %>
{:multipart=>true})do | f |%>
... 作者字段。。。
....................
是的,您需要
简单表单提供的帮助程序(因为您正在为
使用
简单表单)

步骤1

在您的
书籍
模型中,您应该为:author添加
接受嵌套的属性

步骤2

修改books\u controller.rb的新方法

由于您在
图书
模型中拥有
所属的作者
,您的
图书控制器
新方法将是

def new
@book = Book.new
@book.build_author #This is very important
end
步骤3

您的
book_params
方法应修改为

def book_params
params.require(:book).permit(:title, :synopsis, :body, :jacket_cover,author_attributes: [:name,:biography,..,..])
end
步骤4

最后,您的
图书创建的
表单
如下所示

<%= simple_form_for(@book, :html => { :multipart => true }) do |f| %>
  <%= f.error_notification %>

  <div class="inputs">
    <%= f.file_field :jacket_cover %>
    <%= f.input :title %>
    <%= f.input :synopsis %>
    <%= f.input :body %>
  </div>

  <%= f.simple_fields_for :author do |a| %>

  ... author fields...
  ....................
  <% end %>

  <div class="actions">
    <%= f.button :submit %>
  </div>
<% end %>
{:multipart=>true})do | f |%>
... 作者字段。。。
....................

您的问题有些冗长,但我会详细说明您需要知道的内容

--

嵌套对象

是否可以将另一个资源嵌套到已创建的资源中,以便在图书创建页面中添加作者创建?

Rails构建在Ruby(一种语言)之上。这意味着Rails也是面向对象的,因此,如果您想在创建书籍的同时创建作者(这仅适用于
创建
),您需要对您的模型使用指令:

#app/models/book.rb
Class Book < ActiveRecord::Base
   has_one :author
   accepts_nested_attributes_for :author
end

#app/controllers/books_controller.rb
Class BooksController < ApplicationController
   def new
      @book = Book.new
      @book.build_author #-> this will be authors.build if multiple
   end

   def create
      @book = Book.new(book_params)
      @book.save
   end

   private

   def book_params
      params.require(:book).permit(:title, :synopsis, :body, author_attributes: [:biography])
   end
end

这也将创建图书记录和相关作者记录

您的问题有些冗长,但我会详细说明您需要知道的内容

--

嵌套对象

是否可以将另一个资源嵌套到已创建的资源中,以便在图书创建页面中添加作者创建?

Rails构建在Ruby(一种语言)之上。这意味着Rails也是面向对象的,因此,如果您想在创建书籍的同时创建作者(这仅适用于
创建
),您需要对您的模型使用指令:

#app/models/book.rb
Class Book < ActiveRecord::Base
   has_one :author
   accepts_nested_attributes_for :author
end

#app/controllers/books_controller.rb
Class BooksController < ApplicationController
   def new
      @book = Book.new
      @book.build_author #-> this will be authors.build if multiple
   end

   def create
      @book = Book.new(book_params)
      @book.save
   end

   private

   def book_params
      params.require(:book).permit(:title, :synopsis, :body, author_attributes: [:biography])
   end
end

这也将创建图书记录和相关作者记录

检查嗨,安德烈,只是一个关于此的快速查询。当我添加一本新书时,这是否允许我在新书页面上创建作者配置文件?为此,您需要设置模型,反之亦然(这样,作者信息将包含在图书表单中)。意思是使用@book.build\u author in:newaction in books\u controller等等(我将修改答案)嗨,安德烈,请快速查询一下。当我添加一本新书时,这是否允许我在新书页面上创建作者配置文件?为此,您需要设置模型,反之亦然(这样,作者信息将包含在图书表单中)。意思是使用@book.build\u author in:books\u controller中的新操作等等(我将修改答案),以便澄清Pavan,我将能够在图书创建页面中添加作者以及在此处创建图书?您可能应该澄清,
simple\u字段\u来自
simple\u表单
gem。其实这并不是必需的,OP也可以使用
表单u来表示
字段u来表示
,但我认为使用
简单表单
是个好主意:)@nathanvda我已经更新了我的答案,以明确说明:)为了澄清Pavan,我可以在图书创建页面中添加作者,也可以在此处创建图书?您应该澄清,
simple\u字段\u来自
simple\u表单
gem。这并不是必须的,OP也可以使用
表单和
字段,但我认为使用
简单表单是一个好主意:)@nathanvda我更新了我的答案,明确地说:)可以在图书创建页面中添加作者,也可以在这里创建图书?当你说“添加作者”时,你的意思是什么?可以在图书创建页面中添加作者,也可以在此处创建图书?当你说“添加作者”时,你的意思是什么?
<%= form_for(@book, :html => { :multipart => true }) do |f| %>
  <%= f.error_notification %>
  <div class="inputs">
#book fields
  </div>
  <%= f.fields_for :author do |author| %>
#author fields
    <%= f.button :submit %>
  </div>
<% end %>
def new
@book = Book.new
@book.build_author #This is very important
end
def book_params
params.require(:book).permit(:title, :synopsis, :body, :jacket_cover,author_attributes: [:name,:biography,..,..])
end
<%= simple_form_for(@book, :html => { :multipart => true }) do |f| %>
  <%= f.error_notification %>

  <div class="inputs">
    <%= f.file_field :jacket_cover %>
    <%= f.input :title %>
    <%= f.input :synopsis %>
    <%= f.input :body %>
  </div>

  <%= f.simple_fields_for :author do |a| %>

  ... author fields...
  ....................
  <% end %>

  <div class="actions">
    <%= f.button :submit %>
  </div>
<% end %>
#app/models/book.rb
Class Book < ActiveRecord::Base
   has_one :author
   accepts_nested_attributes_for :author
end

#app/controllers/books_controller.rb
Class BooksController < ApplicationController
   def new
      @book = Book.new
      @book.build_author #-> this will be authors.build if multiple
   end

   def create
      @book = Book.new(book_params)
      @book.save
   end

   private

   def book_params
      params.require(:book).permit(:title, :synopsis, :body, author_attributes: [:biography])
   end
end
#app/views/books/new.html.erb
<%= form_for @book do |f| %>
   <%= f.text_field :title %>
   <%= f.text_field :synopsis %>
   <%= f.text_field :body %>

   <%= f.fields_for :author do |a| %>
      <%= a.text_field :biography %>
   <% end %>

   <%= f.submit %>
<% end %>