首页 > 解决方案 > Rails:无法使用 MongoMapper 创建新文档

问题描述

我一直在使用带有 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)

请帮忙!

标签: ruby-on-railsmongomapper

解决方案


Mongoapper 与 rails 5.2 不兼容


推荐阅读