Rails 祖先嵌套形式

发布于 2024-11-14 10:51:55 字数 1360 浏览 4 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗷兮 2024-11-21 10:51:55

今天遇到了同样的问题 - 我相信它是 祖先的限制,查看代码(它会引发此错误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 of children.build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文