我正在使用带有awesome_nested_set的Rails 3.0.7,我正在尝试创建一个嵌套表单,这样我就可以在一个创建表单中输入一个类别和子类别。
这是我的类别模型,控制器&形式
category.rb
class Category < ActiveRecord::Base
acts_as_nested_set
end
categories_controller.rb
class CategoriesController < InheritedResources::Base
def new
@category = Category.new
3.times { @category.children.build(:name => "test") }
end
end
形式
= form_for @category do |f|
-if @category.errors.any?
#error_explanation
%h2= "#{pluralize(@category.errors.count, "error")} prohibited this category from being saved:"
%ul
- @category.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name
= f.text_field :name
%p Sub categories
= f.fields_for :children do |child|
= child.text_field :name
.actions
= f.submit 'Save'
这里的问题是我的表单中只有一个子类别,并且没有将名称设置为'test',因此我不认为它实际上是此处显示的类别的子项。
我在这里缺少什么?
可以这样做吗?
有更简单的方法吗?
更新 如果我将表单更改为以下内容,则会显示三个子类别,每个子类别的名称设置为“test”。但这不会正确保存。
%p Sub categories
- @category.children.each do |sub|
= f.fields_for sub do |child|
= child.label :name
= child.text_field :name
%br
答案 0 :(得分:0)
找到我的答案并在此处写了一个关于它的维基页面:
https://github.com/collectiveidea/awesome_nested_set/wiki/nested-form-for-nested-set