Having an error with nested forms with Mongoid

坚强是说给别人听的谎言 提交于 2020-01-03 06:27:40

问题


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'

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4175206/having-an-error-with-nested-forms-with-mongoid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!