关于 mongoidreferences_many、视图和控制器的问题
我正在通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
accepts_nested_attributes_for :tags
添加到HyperLink
。Try adding
accepts_nested_attributes_for :tags
toHyperLink
.理想情况下,你的参数应该看起来像这样(注意标签的变化):
因为 jdc 已经指出控制器和视图代码对于实际指出问题将非常有帮助。
Ideally your parameters should look something like this(notice the change in tags):
As jdc already pointed controller and view code would be much helpful to actually point out the problem.