【发布时间】:2018-06-07 04:24:20
【问题描述】:
我一直在使用带有 Rails 5.2 的 MongoMapper。 创建模型:
class Item
include MongoMapper::Document
key :title, String
key :description, String
end
在 MongoDB 中,我插入了文档
{
"title": "demo",
"description": "this is the demo item"
}
使用 Rails 控制台测试此模型
Item.where({:title => "demo"}).first
它显示结果是上面的文件。 但是当我创建一个新文档时:
item = Item.create({:title => "demo2", :description => "the demo2"})
控制台出错
Traceback (most recent call last):
2: from (irb):3
1: from (eval):9:in `title='
NoMethodError (undefined method `[]' for nil:NilClass)
我尝试了new的方法
item = Item.new
控制台显示:
Traceback (most recent call last):
2: from (irb):4
1: from (irb):4:in `new'
NoMethodError (undefined method `keys' for nil:NilClass)
请帮忙!
【问题讨论】: