Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Mongoid时,如何在json响应中包含根?_Ruby On Rails_Json_Mongoid - Fatal编程技术网

Ruby on rails 使用Mongoid时,如何在json响应中包含根?

Ruby on rails 使用Mongoid时,如何在json响应中包含根?,ruby-on-rails,json,mongoid,Ruby On Rails,Json,Mongoid,使用ActiveRecord似乎很容易: config.active_record.include_root_in_json = true 但是我找不到合适的Mongoid文档以下是您需要的文档: 根据config/mongoid.yml中的文档,您只需设置: 将_root_包含在_json中:true 干杯以下是您需要的文档: 根据config/mongoid.yml中的文档,您只需设置: 将_root_包含在_json中:true Rails 4中的cheers在mongoid.y

使用ActiveRecord似乎很容易:

config.active_record.include_root_in_json = true

但是我找不到合适的Mongoid文档

以下是您需要的文档:

根据config/mongoid.yml中的文档,您只需设置:

将_root_包含在_json中:true


干杯

以下是您需要的文档:

根据config/mongoid.yml中的文档,您只需设置:

将_root_包含在_json中:true


Rails 4中的cheers

在mongoid.yml中使用include_root_in_json设置的jbuilder没有添加包装器。我更新了jbuilder模板,如下所示:

# wrap scaffolded template with set!
# people/index.json

json.set! :people do
  json.array!(@people) do |p|
    json.extract! p, :name, :age
  end
end #=> returns {"people" : [ "name" : "Jane", "age" : 30 ] }

在Rails 4中,jbuilder在mongoid.yml中使用include_root_in_json设置时没有添加包装器。我更新了jbuilder模板,如下所示:

# wrap scaffolded template with set!
# people/index.json

json.set! :people do
  json.array!(@people) do |p|
    json.extract! p, :name, :age
  end
end #=> returns {"people" : [ "name" : "Jane", "age" : 30 ] }