使用 Mongoid 进行嵌套表单时出现错误

发布于 2024-10-03 02:02:11 字数 1045 浏览 4 评论 0原文

我正在 Rails 3 上使用 Mongoid 并尝试构建嵌套表单。我遇到的问题是,当我使用 Accept_nested_attributes_for 方法时,我收到此错误消息:

undefined method `persisted?' for []:Array

是否还有其他人在 Mongoid 中遇到嵌套表单问题?我能做什么来修复它?

编辑:有关错误的更多信息。这是完整的踪迹。

mongoid (2.0.0.beta.19) lib/mongoid/criteria.rb:156:in `send'
mongoid (2.0.0.beta.19) lib/mongoid/criteria.rb:156:in `method_missing'
mongoid (2.0.0.beta.19) lib/mongoid/associations/references_many.rb:113:in `send'
mongoid (2.0.0.beta.19) lib/mongoid/associations/references_many.rb:113:in `method_missing'
actionpack (3.0.1) lib/action_view/helpers/form_helper.rb:1280:in `fields_for_nested_model'
actionpack (3.0.1) lib/action_view/helpers/form_helper.rb:1273:in `fields_for_with_nested_attributes'
actionpack (3.0.1) lib/action_view/helpers/form_helper.rb:1155:in `fields_for'
simple_form (1.2.2) lib/simple_form/action_view_extensions/builder.rb:96:in `simple_fields_for'
app/views/users/profile.html.haml:16:in `_app_views_users_profile_html_haml__599717908_2187897020_0'

I am using Mongoid on Rails 3 and trying to build a nested form. The problem I run into is when I used accept_nested_attributes_for method, I get this error message:

undefined method `persisted?' for []:Array

Is anyone else having problem with nested forms in Mongoid? What can I do to fix it?

Edit: more about the error. This is the full trace.

mongoid (2.0.0.beta.19) lib/mongoid/criteria.rb:156:in `send'
mongoid (2.0.0.beta.19) lib/mongoid/criteria.rb:156:in `method_missing'
mongoid (2.0.0.beta.19) lib/mongoid/associations/references_many.rb:113:in `send'
mongoid (2.0.0.beta.19) lib/mongoid/associations/references_many.rb:113:in `method_missing'
actionpack (3.0.1) lib/action_view/helpers/form_helper.rb:1280:in `fields_for_nested_model'
actionpack (3.0.1) lib/action_view/helpers/form_helper.rb:1273:in `fields_for_with_nested_attributes'
actionpack (3.0.1) lib/action_view/helpers/form_helper.rb:1155:in `fields_for'
simple_form (1.2.2) lib/simple_form/action_view_extensions/builder.rb:96:in `simple_fields_for'
app/views/users/profile.html.haml:16:in `_app_views_users_profile_html_haml__599717908_2187897020_0'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

心头的小情儿 2024-10-10 02:02:11

Rails 3 遵循 ActiveModel,它具有一定的契约,如 ActionPack(和 ActionView)所期望的那样。

请参阅这篇文章,, Yehuda Katz 在 ActiveModel 部分详细解释了这一点。另请参阅 ActiveModel ChangeLog,其中引用了该方法。

简而言之,您的模型必须实现某些方法才能与 Rails 3 应用程序的其余部分良好配合。而这个方法就是其中之一。

ActiveRecord 3 就遵循这个契约。如果你扔掉它并使用你自己的 ORM(如 Mongoid),除非框架这样做(如 mongoid 情况),否则你必须显式地执行此操作。 couch_potato 是 couchdb 的类似包装器,其作用完全相同。

Rails 3 follows ActiveModel which has certain contract to be followed as expected by ActionPack (and ActionView).

Refer to this post,, in which Yehuda Katz explains this in detail in ActiveModel section. Also refer to ActiveModel ChangeLog, which has reference to the method.

Simply put, your model has to implement certain methods to play nice with rest of the rails 3 app. And this method is one of them.

ActiveRecord 3 just follows this contract. If you are throwing it away and using your own ORM like Mongoid, unless the framework does this (like mongoid case), you have to do it explicitly. couch_potato which is a similar wrapper for couchdb does exactly the same.

江南烟雨〆相思醉 2024-10-10 02:02:11

最近 我在 Github 中找到的拉取请求通过 wink 修复了此问题。

This issue was fixed by a recent pull request I found in Github by wink.

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