简体   繁体   English

从RABL移除节点

[英]Removing node from RABL

I'm new to rails and I'm trying to create an API for my application. 我是Rails的新手,正在尝试为我的应用程序创建API。 I'm also using rabl for generating my JSON responses. 我还使用rabl生成我的JSON响应。 In my rabl template I want to send a timestamp and a collection of items. 在我的rabl模板中,我想发送一个时间戳和一组项目。

The code looks like this: 代码如下:

object false
node(:timestamp){CourseType.maximum("updated_at")}
child(@course_types){attributes :id, :name, :deleted}

And I want to get an object that would look like this: 我想要一个看起来像这样的对象:

{"course_types":
    [{"id":2,"name":"Driving","deleted":false},
     {"id":4,"name":"Cooking","deleted":false}],
 "timestamp":"2016-04-25 16:22:57 UTC"}

However I'm getting this instead: 但是我得到了这个:

{"course_types":
    [{"course_type":
        {"id":2,"name":"Driving","deleted":false}},
     {"course_type":
        {"id":4,"name":"Cooking","deleted":false}}],
 "timestamp":"2016-04-25 16:22:57 UTC"}

Any ideas on how to solve this? 关于如何解决这个问题的任何想法?

Thanks 谢谢

Rabl allows you to control these children root nodes across all views via an initializer Rabl允许您通过初始化程序在所有视图中控制这些子根节点

https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-mysql-server-part-2 https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-mysql-server-part-2

and then setting the correct combination of the configurations 然后设置正确的配置组合

# config/initializers/rabl_init.rb
require 'rabl'
Rabl.configure do |config|
  config.include_json_root = true
  config.include_child_root = false  
end

This might be a duplicate of these questions 这可能是这些问题的重复

Rabl, remove parent element of children Rabl,删除孩子的父元素

Removing child root nodes in RABL 在RABL中删除子根节点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM