如何在AMS的方括号内呈现不带括号的json变量

时间:2019-07-18 21:52:17

标签: ruby-on-rails ruby ruby-on-rails-5 active-model-serializers

我想知道是否可以在带有AMS括号的渲染器上渲染不带参数的变量?

例如:

render json: { comments: @comments, status: 200, hasMore: hasMore }

我只想呈现不带注释的@comments:并在括号中显示hasMore:hasMore,这可能只是在括号内显示!

通过这种方式

render json: @comments, hasMore: hasMore,  include: ['user', 'replies.**']

没有在json上显示hasMore

1 个答案:

答案 0 :(得分:0)

如果要渲染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