Rails 祖先嵌套形式
我刚刚开始使用 Ancestry 而不是 Awesome_nested_set,我想创建一个嵌套表单,以便我可以在一个表单中创建一个父帐户和许多子帐户。问题似乎是 Ancestry 不允许您为新父母创建孩子。
在 Awesome_nested_set 中,我可以在 Rails 控制台中执行此操作,
a = Account.new
a.children.build
当我输入 a.children 时,即使我的帐户父级尚未创建,我也可以看到其中的新子级。这使我能够显示一个包含父帐户和一些空白子项的表单,然后在提交时我会忽略任何空白子项并创建整个批次。
如果我尝试使用祖先做同样的事情,我会收到以下错误:
a=Account.new
a.children
Ancestry::AncestryException: No child ancestry for new record. Save record before performing tree operations.
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/ancestry-1.2.4/lib/ancestry/instance_methods.rb:62:in `child_ancestry'
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/ancestry-1.2.4/lib/ancestry/instance_methods.rb:132:in `child_conditions'
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/ancestry-1.2.4/lib/ancestry/instance_methods.rb:136:in `children'
from (irb):8
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
这是祖先的限制吗? 我有一种不同的方式可以使用祖先创建嵌套表单吗?
I've just started using Ancestry instead of awesome_nested_set and I would like to create a nested form so that I can create a parent account and many children accounts all in the one form. The problem seems to be that Ancestry doesn't allow you to create a child for a new parent.
In awesome_nested_set I could do this in the rails console
a = Account.new
a.children.build
When I type a.children I can see that new child in there even though my account parent hasn't been created yet. This allowed me to display a form with the parent account and a few blank children, then on submit I would just ignore any blank children and create the whole lot.
If I try and do the same using ancestry I get the following error:
a=Account.new
a.children
Ancestry::AncestryException: No child ancestry for new record. Save record before performing tree operations.
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/ancestry-1.2.4/lib/ancestry/instance_methods.rb:62:in `child_ancestry'
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/ancestry-1.2.4/lib/ancestry/instance_methods.rb:132:in `child_conditions'
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/ancestry-1.2.4/lib/ancestry/instance_methods.rb:136:in `children'
from (irb):8
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /home/map7/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Is this a limitation of ancestry?
I there a different way in which I could create a nested form using ancestry?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
今天遇到了同样的问题 - 我相信它是 祖先的限制,查看代码(它会引发此错误
if new_record?
)。虽然这并不理想,但目前我正在使用
children.create
而不是children.build
。Ran into the same problem today - I believe it to be a limitation of ancestry, looking at the code (it raises this error
if new_record?
).While it's not ideal, at the moment I'm using
children.create
instead ofchildren.build
.