Rails 3.1 include_root_in_json [英] Rails 3.1 include_root_in_json

查看:53
本文介绍了Rails 3.1 include_root_in_json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ActiveRecord::Base.include_root_in_json = true 在 rails 3.10.rc4 中似乎不起作用,我在文档中也没有看到.

既然根元素现在默认是关闭的,我们如何重新启用它?

@comments.to_json 在 rails 3.1 现在看起来像

<预><代码>[{评论:有趣的街头公园.",created_at: 2011-06-29T02:28:29Z,}]

在以前的版本中,它具有我需要取回的根节点.

<预><代码>[{评论: {评论:有趣的街头公园.",created_at: 2011-06-29T02:28:29Z}}]

解决方案

尝试直接在您的 Comment 模型上设置.

class 注释 

ActiveRecord::Base.include_root_in_json = true doesn't seem to be working in rails 3.10.rc4 and I don't see it in the docs.

Since the root element is now off by default how do we re-enable it?

@comments.to_json in rails 3.1 now looks like

[
  {
    comment: "Fun street park.",
    created_at: 2011-06-29T02:28:29Z,
  }
]

And in previous versions it has the root node which I need to get back.

[
  {
    comment: {
      comment: "Fun street park.",
      created_at: 2011-06-29T02:28:29Z
    }
  }
]

解决方案

Try setting this directly on your Comment model.

class Comment < ActiveRecord::Base
  self.include_root_in_json = true
end

这篇关于Rails 3.1 include_root_in_json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆