加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – rails ancestry嵌套表单

发布时间:2020-12-17 03:12:59 所属栏目:百科 来源:网络整理
导读:我刚刚开始使用Ancestry而不是awesome_nested_set,我想创建一个嵌套表单,以便我可以在一个表单中创建一个父帐户和许多子帐户.问题似乎是Ancestry不允许你为新的父母创建一个孩子. 在awesome_nested_set中,我可以在rails控制台中执行此操作 a = Account.newa.
我刚刚开始使用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>'

这是祖先的限制吗?
我有不同的方式可以使用祖先创建嵌套表单?

解决方法

今天遇到同样的问题 – 我相信它是一个 limitation of ancestry,看着代码(如果new_record会引发这个错误?).

虽然它不理想,但目前我正在使用children.create而不是children.build.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读