Having an error with nested forms with Mongoid

2019-06-12 04:29发布

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'

2条回答
祖国的老花朵
2楼-- · 2019-06-12 04:53

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.

查看更多
The star\"
3楼-- · 2019-06-12 05:02

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

查看更多
登录 后发表回答