Search code examples
ruby-on-railsjbuilder

rails: jbuilder returns an array of 1 json instead of a json object


I have the following simple jbuilder template

# temp.jbuilder
json.home_id                    command[:home_id]
json.query                      command[:query]
json.query_param                command[:query_param]

When I x = render "temp", format: :json, locals: { command: { home_id: 0, query: "QQQ", query_param: "P" } }

I always get an array in x, instead of getting a json object. Can you tell me why? is it the expected response?


Solution

  • You should use render_to_string instead of render:

    If you want to see the exact results of a call to render without needing to inspect it in a browser, you can call render_to_string. This method takes exactly the same options as render, but it returns a string instead of sending a response back to the browser.