Rails:字段被定义为一个(n)数组,但收到了 ActiveSupport::HashWithIn DifferentAccess
我创建了一个 Post 和一个 TagObject 模型,如下所示
class Post
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :tag_objects
#embeds_many :comments
references_one :uploader, :class_name => 'User'
mount_uploader :image, ImageUploader
validates_presence_of :image
attr_accessible :tag_objects, :image
end
class TagObject
include Mongoid::Document
field :name
field :tags, :type => Array
embedded_in :post, :inverse_of => :tag_objects
attr_accessible :name, :tags
end
,当前有一个页面将 PUT 提交到 Post 控制器的更新方法。更新失败,我在 WEBrick 控制台中收到以下信息。
Started POST "/posts/4d4a174fa729cf71c70000a8" for 127.0.0.1 at Wed Feb 02 21:52:09 -0500 2011
Processing by PostsController#update as HTML
Parameters: {"post"=>{"tag_objects"=>{"1"=>{"tags"=>{"1"=>"testingfds"}}}}, "authenticity_token"=>"OZ+eXzD5NyqUI4CzPadlFUMDwRrg4LsaQBs5i+J65tU=", "id"=>"4d4a174fa729cf71c70000a8"}
honeycomb_development['posts'].find({:_id=>BSON::ObjectId('4d4a174fa729cf71c70000a8')}, {}).limit(-1)
Completed in 2ms
Mongoid::Errors::InvalidType (Field was defined as a(n) Array, but received a ActiveSupport::HashWithIndifferentAccess with the value {"1"=>"testingfds"}.):
app/controllers/posts_controller.rb:39:in `update'
我完全不知道如何解决它,任何帮助将不胜感激。
I've created a Post and a TagObject model as follows
class Post
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :tag_objects
#embeds_many :comments
references_one :uploader, :class_name => 'User'
mount_uploader :image, ImageUploader
validates_presence_of :image
attr_accessible :tag_objects, :image
end
class TagObject
include Mongoid::Document
field :name
field :tags, :type => Array
embedded_in :post, :inverse_of => :tag_objects
attr_accessible :name, :tags
end
and currently have a page submit a PUT to the update method of the Post controller. The update fails and I get the following in the WEBrick console.
Started POST "/posts/4d4a174fa729cf71c70000a8" for 127.0.0.1 at Wed Feb 02 21:52:09 -0500 2011
Processing by PostsController#update as HTML
Parameters: {"post"=>{"tag_objects"=>{"1"=>{"tags"=>{"1"=>"testingfds"}}}}, "authenticity_token"=>"OZ+eXzD5NyqUI4CzPadlFUMDwRrg4LsaQBs5i+J65tU=", "id"=>"4d4a174fa729cf71c70000a8"}
honeycomb_development['posts'].find({:_id=>BSON::ObjectId('4d4a174fa729cf71c70000a8')}, {}).limit(-1)
Completed in 2ms
Mongoid::Errors::InvalidType (Field was defined as a(n) Array, but received a ActiveSupport::HashWithIndifferentAccess with the value {"1"=>"testingfds"}.):
app/controllers/posts_controller.rb:39:in `update'
I have absolutely no idea how to fix it and any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,问题似乎在于它正在尝试执行
而不是
像我预期的那样执行并设置其内容。我只是在更新方法中添加了一些逻辑来执行此操作,并且效果很好。
Yah, seems the problem was that it was trying to do
instead of doing
and set it's contents like I was expecting. I just added some logic into the update method to do so, and it works fine.
在模型中,
将
In the model, put
instead of