Search code examples
ruby-on-railsrubyruby-on-rails-5active-model-serializers

How to render json some variables without brackets inside brackets on AMS


I'd like to know if is possible to render variable without params on render with brackets with AMS?

for exemple:

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

i'd like to render just @comments without the comments: and with hasMore: hasMore showing on the json that is just possible inside brackets!

with this way

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

did not show the hasMore on the json


Solution

  • If you're rendering json, you need to make sure what you pass as json is actually valid json. Here's a most simple example:

    # this does not work and will throw a syntax error
    render json: 'foo: 'foo', bar: 'bar'
    

    but

    # 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