关于 mongoidreferences_many、视图和控制器的问题

发布于 2024-10-29 18:08:12 字数 905 浏览 0 评论 0原文

我正在通过 Mongoid 在 Rails 3 中使用 MongoDB。我已经定义了下面的类,但是当我尝试通过脚手架视图创建新的超链接时,出现错误。我相信发生的情况是 Tags 数组没有得到正确处理。我正在使用默认的控制器支架。我需要做什么来确保 mongoid 知道如何添加标签?

class Hyperlink
  include Mongoid::Document
  field :name
  field :url
  embeds_many :comments

  references_many :tags

  validates_presence_of :name, :url
  validates_uniqueness_of :name, :url
end

class Tag
  include Mongoid::Document

  field :name
  validates_uniqueness_of :name
  referenced_in :hyperlink    
end

控制器响应 HyperlinksController#create 中出现类型错误

can't convert Symbol into Integer

**Request**

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"yn5SwZPBIMcpzrGQeO9t3tJ2Y2Q6nlsDBPbI43ahj0k=",
 "hyperlink"=>{"name"=>"Stack Overflow",
 "link"=>"http:://www.stackoverflow.com",
 "tags"=>{"tag"=>"programming"}},
 "commit"=>"Create Hyperlink"}

I'm playing with MongoDB in Rails 3 via Mongoid. I've defined the classes below, but when I attempt to create a new Hyperlink via the scaffolded view I get an error. I believe what is happening is that the Tags array is not being handled properly. I'm using the default controller scaffold. What do I need to do to ensure mongoid knows how to add the tags?

class Hyperlink
  include Mongoid::Document
  field :name
  field :url
  embeds_many :comments

  references_many :tags

  validates_presence_of :name, :url
  validates_uniqueness_of :name, :url
end

class Tag
  include Mongoid::Document

  field :name
  validates_uniqueness_of :name
  referenced_in :hyperlink    
end

Controller response
TypeError in HyperlinksController#create

can't convert Symbol into Integer

**Request**

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"yn5SwZPBIMcpzrGQeO9t3tJ2Y2Q6nlsDBPbI43ahj0k=",
 "hyperlink"=>{"name"=>"Stack Overflow",
 "link"=>"http:://www.stackoverflow.com",
 "tags"=>{"tag"=>"programming"}},
 "commit"=>"Create Hyperlink"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

久随 2024-11-05 18:08:12

尝试将 accepts_nested_attributes_for :tags 添加到 HyperLink

Try adding accepts_nested_attributes_for :tags to HyperLink .

弥枳 2024-11-05 18:08:12

理想情况下,你的参数应该看起来像这样(注意标签的变化):

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"yn5SwZPBIMcpzrGQeO9t3tJ2Y2Q6nlsDBPbI43ahj0k=",
 "hyperlink"=>{"name"=>"Stack Overflow",
 "link"=>"http:://www.stackoverflow.com",
 "tags"=>{"0" => {"name"=>"programming", :id => "xxxx"}}},
 "commit"=>"Create Hyperlink"}

因为 jdc 已经指出控制器和视图代码对于实际指出问题将非常有帮助。

Ideally your parameters should look something like this(notice the change in tags):

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"yn5SwZPBIMcpzrGQeO9t3tJ2Y2Q6nlsDBPbI43ahj0k=",
 "hyperlink"=>{"name"=>"Stack Overflow",
 "link"=>"http:://www.stackoverflow.com",
 "tags"=>{"0" => {"name"=>"programming", :id => "xxxx"}}},
 "commit"=>"Create Hyperlink"}

As jdc already pointed controller and view code would be much helpful to actually point out the problem.

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