使用 Mongoid 进行嵌套表单时出现错误
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
最近 我在 Github 中找到的拉取请求通过 wink 修复了此问题。
This issue was fixed by a recent pull request I found in Github by wink.