Ruby on rails 在Rails中以嵌套形式更新子对象
我的目标是一个部分。每个部分可以有其他部分。 节响应#索引,索引返回其顺序索引。如果节没有同级节,则为0 假设我的数据结构是:Ruby on rails 在Rails中以嵌套形式更新子对象,ruby-on-rails,Ruby On Rails,我的目标是一个部分。每个部分可以有其他部分。 节响应#索引,索引返回其顺序索引。如果节没有同级节,则为0 假设我的数据结构是: a / \ b c 其中: a、 索引=0 b、 索引=0 c、 索引=1 如果导航到/sections/a/manage child ordering,我希望能够编辑a子部分的顺序。在我看来,这就是代码: <% if @section.children %> <%= form_for @section do |child
a
/ \
b c
其中:
a、 索引=0
b、 索引=0
c、 索引=1
如果导航到/sections/a/manage child ordering
,我希望能够编辑a
子部分的顺序。在我看来,这就是代码:
<% if @section.children %>
<%= form_for @section do |child| %>
<%= f.fields_for :children, @section.children do |c| %>
<%= c.text_field :index %>
<% end %>
<% end %>
<% end %>
<%= f.submit 'Save', class: 'btn-submit' %>
我得到的错误是:
undefined method `errors' for #<Tag::ActiveRecord_Associations_CollectionProxy:0x007f23e60bdbd0>
的未定义方法“错误”#
我做错了什么?
的表单不应采取集合,而应采取负责实施表单操作的单一模型。对于您来说,这可能是@部分
ie
= form_for @section do |f|
将此添加到模型中实现了以下目的:
belongs_to :parent, class_name: 'Section'
has_many :children, class_name: 'Section', foreign_key: :parent_id
accepts_nested_attributes_for :children
更新并更新了我收到的新错误消息。然后你必须显示更多的代码,比如你在哪里设置@section
var(以及如何设置)。你指的是模型吗?