I've got a bar graph that I'm trying to populate with a JSON
response. I'm using Rails 5 with the built in JBuilder functionality.
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:
{"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_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.
Here are the contents of my graph_data.json.jbuilder
file.
json.array! @graph_data
Thanks in advance.
In your jbuilder file, replace
json.array! @graph_data
to
json.graph_type @graph_data[:graph_type]
json.months @graph_data[:months]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.