rails: jbuilder returns 1 json 的数组而不是 json 对象

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

我有以下简单的 jbuilder 模板

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

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

我总是在 x 中得到一个数组,而不是得到一个 json 对象。你能告诉我为什么吗?这是预期的反应吗?

您应该使用 render_to_string 而不是 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.