ruby-on-rails - 如何在AMS上的括号内渲染json一些没有括号的变量
问题描述
我想知道是否可以在没有参数的情况下使用带有 AMS 的括号渲染变量?
举个例子:
render json: { comments: @comments, status: 200, hasMore: hasMore }
我想只渲染没有评论的@comments:并在json上显示hasMore:hasMore,这可能在括号内!
用这种方式
render json: @comments, hasMore: hasMore, include: ['user', 'replies.**']
没有在 json 上显示 hasMore
解决方案
如果要渲染 json,则需要确保作为 json 传递的内容实际上是有效的 json。这是一个最简单的例子:
# this does not work and will throw a syntax error
render json: 'foo: 'foo', bar: 'bar'
但
# this works
render json: {foo: 'foo', bar: 'bar'}
# this works because it will render an array type
render json: @comments # assuming that is a collection
推荐阅读
- javascript - 为什么 vue 更改特定数组成员不更新 dom
- python - 在 Jinja 中创建对象/函数以更好地重用代码
- c# - 使用 C# 中 XML 文档内部引用的 XSD 文件验证 XML 文件
- python - 批量平均和全局 Fscore 之间的差异
- javascript - Bootstrap 4 模态不会关闭
- excel - 每 n 列复制每 n 个单元格并粘贴到新行中,对于每一行
- c++ - 将 dynamic_cast 与 std::exception 一起使用
- pandas - 用 groupby 计算条件中位数
- sql - 如何获取汇总的月度数据并计算新字段?
- macos - 文件删除自身的Shell命令