【问题标题】:How to render json some variables without brackets inside brackets on AMS如何在AMS上的括号内渲染json一些没有括号的变量
【发布时间】:2019-07-18 21:52:17
【问题描述】:

我想知道是否可以在使用 AMS 的括号中渲染不带参数的变量?

例如:

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

这样

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

没有在 json 上显示 hasMore

【问题讨论】:

  • 那行不通。您需要构建一个 json 类型的对象才能正确呈现它。

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


【解决方案1】:

如果您正在渲染 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

【讨论】:

    猜你喜欢
    • 2011-05-14
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多