【问题标题】:Removing node from RABL从 RABL 中删除节点
【发布时间】:2016-04-26 23:53:43
【问题描述】:

我是 Rails 新手,我正在尝试为我的应用程序创建一个 API。我还使用 rabl 来生成我的 JSON 响应。在我的 rabl 模板中,我想发送一个时间戳和一组项目。

代码如下:

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

我想得到一个看起来像这样的对象:

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

但是我得到了这个:

{"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"}

关于如何解决这个问题的任何想法?

谢谢

【问题讨论】:

    标签: ruby-on-rails json rabl


    【解决方案1】:

    Rabl 允许您通过初始化程序在所有视图中控制这些子根节点

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

    然后设置正确的配置组合

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

    这可能与这些问题重复

    Rabl, remove parent element of children

    Removing child root nodes in RABL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多