Rails:字段被定义为一个(n)数组,但收到了 ActiveSupport::HashWithIn DifferentAccess

发布于 2024-10-15 11:11:40 字数 1348 浏览 2 评论 0原文

我创建了一个 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 技术交流群。

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

发布评论

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

评论(2

绝不服输 2024-10-22 11:11:40

是的,问题似乎在于它正在尝试执行

tags = {0=>'testingdfg'}

而不是

tags[0] = 'testingdfg'

像我预期的那样执行并设置其内容。我只是在更新方法中添加了一些逻辑来执行此操作,并且效果很好。

Yah, seems the problem was that it was trying to do

tags = {0=>'testingdfg'}

instead of doing

tags[0] = 'testingdfg'

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.

止于盛夏 2024-10-22 11:11:40

在模型中,

field :tags, :type => Hash

field :tags, :type => Array

In the model, put

field :tags, :type => Hash

instead of

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