Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 嵌套模型/表单不';不要创建条目_Ruby On Rails_Ruby_Rails Activerecord_Nested Forms - Fatal编程技术网

Ruby on rails 嵌套模型/表单不';不要创建条目

Ruby on rails 嵌套模型/表单不';不要创建条目,ruby-on-rails,ruby,rails-activerecord,nested-forms,Ruby On Rails,Ruby,Rails Activerecord,Nested Forms,在深入研究ruby的嵌套模型时,我遇到了一个问题 考虑以下场景 我有以下型号: 作者 书 具有以下规格: 作者: 这就是我尝试使用的表单: def new_book @author = Author.find(params[:id]) end def create_book @author = Author.find(params[:id]) if @author.books.create(params[:book]).save redirect_to action:

在深入研究ruby的嵌套模型时,我遇到了一个问题

考虑以下场景

我有以下型号:

  • 作者
具有以下规格:

作者:

这就是我尝试使用的表单:

def new_book
  @author = Author.find(params[:id])
end

def create_book
  @author = Author.find(params[:id])
  if @author.books.create(params[:book]).save
    redirect_to action: :show
  else
    render :new_book
  end
end
<h1>Add new book to <%= @author.name %>'s collection</h1>
<%= form_for @author, html: { class: "well" } do |f| %>
    <%= fields_for :books do |b| %>
        <%= b.label :name %>
        <%= b.text_field :name %>
        <br/>
        <%= b.label :year %>
        <%= b.number_field :year %>
    <% end %>
    <br/>
    <%= f.submit "Submit", class: "btn btn-primary" %>
    <%= f.button "Reset", type: :reset, class: "btn btn-danger" %>
<% end %>
将新书添加到的收藏中


问题: 当我输入数据并单击“提交”时,它甚至会将我重定向到正确的作者,但不会为该作者保存新记录。
经过大量研究,我似乎无法发现我在这里做错了什么。

将作者更改为:

def new_book
  @author = Author.find(params[:id])
  @book = Book.new
end
请将表格发送至:

<h1>Add new book to <%= @author.name %>'s collection</h1>
<%= form_for ([@author, @book]), html: { class: "well" } do |f| %>

将控制器更改为:

def new_book
  @author = Author.find(params[:id])
  @book = Book.new
end
请将表格发送至:

<h1>Add new book to <%= @author.name %>'s collection</h1>
<%= form_for ([@author, @book]), html: { class: "well" } do |f| %>

你错过了几件事

控制器:

...
def new_book
  @author = Author.find(params[:id])
  @author.books.build
end
...
视图,它是的字段,而不仅仅是的字段:

<%= f.fields_for :books do |b| %>
  <%= b.label :name %>
  <%= b.text_field :name %>
  <br/>
  <%= b.label :year %>
  <%= b.number_field :year %>
<% end %>



您缺少一些东西

控制器:

...
def new_book
  @author = Author.find(params[:id])
  @author.books.build
end
...
视图,它是的字段,而不仅仅是的字段:

<%= f.fields_for :books do |b| %>
  <%= b.label :name %>
  <%= b.text_field :name %>
  <br/>
  <%= b.label :year %>
  <%= b.number_field :year %>
<% end %>



您还需要在您的作者模型可访问方法上添加
:书籍的嵌套属性。Author控制器上的create方法不需要任何代码添加即可开始


注意:您将Books controller设置为在成功时呈现“Books#show”。如果应用程序将您重定向到作者,则表示作者控制器正在处理书籍的创建,除非您将其设置为重定向到作者而不是书籍

您还需要在您的作者模型可访问方法上添加
:书籍的嵌套\u属性\u
。Author控制器上的create方法不需要任何代码添加即可开始


注意:您将Books controller设置为在成功时呈现“Books#show”。如果应用程序将您重定向到作者,则表示作者控制器正在处理书籍的创建,除非您将其设置为重定向到作者而不是书籍

显然这改变了我的路由机制。。。现在它给了我
未定义的方法“author\u books\u path”
,并引用了表单的第二行…请参阅它从头开始实现了子表单。显然,这改变了我的路由机制。。。它现在给了我
未定义的方法“author\u books\u path”
,并引用了表单的第二行…请参阅它从头开始实现了子表单。这看起来已经更好了,但现在它要求我在books controller中创建方法-知道为什么吗?看起来已经更好了,但是现在它要求我在books controller中创建一个方法——知道为什么吗?