[英]Unexpected JSON response - Ruby on Rails
I've got a bar graph that I'm trying to populate with a JSON
response. 我有一个尝试用
JSON
响应填充的条形图。 I'm using Rails 5 with the built in JBuilder functionality. 我正在使用带有内置JBuilder功能的Rails 5。
Here is my code: 这是我的代码:
def graph_data
@customers = Customer.all
@graph_data = {graph_type: "Customer Growth"}
@months = {}
x = 5
while x >= 0
@months[x.months.ago.strftime("%B")] = current_account.customers.where(:created_at => x.months.ago.beginning_of_month..x.months.ago.end_of_month).count
x = x - 1
end
@graph_data[:months] = @months
puts @graph_data.to_json
end
The line puts @graph_data.to_json
outputs the following text: 该行
puts @graph_data.to_json
输出以下文本:
{"graph_type":"Customer Growth","months":{"November":0,"December":0,"January":0,"February":0,"March":0,"April":3}}
as I would expect. 正如我所期望的。
However, when I output the actual JSON response in my graph_data.json.jbuilder
file the output is 但是,当我在
graph_data.json.jbuilder
文件中输出实际的JSON响应时,输出为
[["graph_type","Customer Growth"],["months",{"November":0,"December":0,"January":0,"February":0,"March":0,"April":3}]]
My goal was to get a response that looked more like this: 我的目标是得到一个看起来更像这样的响应:
{"graph_type":"Customer Growth","months":["November":0,"December":0,"January":0,"February":0,"March":0,"April":3]}
but I don't know how to accomplish this with jBuilder. 但是我不知道如何用jBuilder完成。
Here are the contents of my graph_data.json.jbuilder
file. 这是我的
graph_data.json.jbuilder
文件的内容。
json.array! @graph_data
Thanks in advance. 提前致谢。
In your jbuilder file, replace 在您的jbuilder文件中,替换
json.array! @graph_data
to 至
json.graph_type @graph_data[:graph_type]
json.months @graph_data[:months]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.