Reputation: 7380
I have an array of hashes returned from the below map function. I'm trying to populate data for a graph (I'm using AM Charts), which takes data in a specific format mentioned below-
Sample structure of a company record:
1.9.3p327 :073 > Company
=> Company(id: integer, name: string, contact_no: integer, email_id: string, website: string, fax_no: integer, created_at: datetime, updated_at: datetime, divisions_count: integer)
@all_companies = Company.all
Map function:
@all_companies = @all_companies.map {|each_company| { Country: each_company.name, Visits: each_company.divisions_count} }
Output format of above query:
[{:country=>"Samsung", :visits=>8}, {:country=>"Natraj", :visits=>2}, {:country=>"Tupperware", :visits=>5}, {:country=>"Transcen", :visits=>0}, {:country=>"camlin", :visits=>0}]
Graph data input format:
var chartData = [{
country: "USA",
visits: 4025
}, {
country: "China",
visits: 1882
}, {
country: "Japan",
visits: 1809
}, {
country: "Germany",
visits: 1322
}]
I'm using Ruby 1.9.3-p327. I need to convert my output array to the specific graph format. I try going about the same through the following steps:
1.9.3p327 :070 > @all_companies = @all_companies.to_s.gsub(":","")
=> "[{country=>\"Samsung\", visits=>8}, {country=>\"Natraj\", visits=>2}, {country=>\"Tupperware\", visits=>5}, {country=>\"Transcen\", visits=>0}, {country=>\"camlin\", visits=>0}]"
1.9.3p327 :071 > @all_companies = @all_companies.gsub("=>",": ")
=> "[{country: \"Samsung\", visits: 8}, {country: \"Natraj\", visits: 2}, {country: \"Tupperware\", visits: 5}, {country: \"Transcen\", visits: 0}, {country: \"camlin\", visits: 0}]"
1.9.3p327 :072 > puts @all_companies
[{country: "Samsung", visits: 8}, {country: "Natraj", visits: 2}, {country: "Tupperware", visits: 5}, {country: "Transcen", visits: 0}, {country: "camlin", visits: 0}]
=> nil
1.9.3p327 :073 >
Now when I'm trying to display the data in my graph, I'm getting the below syntax error:
var chartData = [{country: "Samsung", visits: 8}, {country: "Natraj", visits: 2}, {country: "Tupperware", visits: 5}, {country: "Transcen", visits: 0}, {country: "camlin", visits: 0}]
The syntax error from the Firebug console now points to " in the above data.
Any work-arounds that could get me through the last step so that I can get the desired graph format?
I need the data in following format:
var chartData = [
{
country: "Samsung",
visits: 8
},
{
country: "Natraj",
visits: 2
},
{
country: "Tupperware",
visits: 5
},
{
country: "Transcen",
visits: 0
},
{
country: "camlin",
visits: 0
}
]
Upvotes: 1
Views: 150
Reputation: 7380
I figured it out thanks to this question.
This is what I do to overcome the " syntax error.
In the controller:-
def company_division_stats
@all_companies = @companies_data = Company.all
@specific_details = @all_companies = @all_companies.map {|each_company| { country: each_company.name, visits: each_company.divisions_count} }
#@all_companies = @all_companies.to_s.gsub(":","")
#@all_companies = @all_companies.gsub("=>",": ")
respond_to do |format|
unless @all_companies.nil?
format.html
format.json { render json: @specific_details, status: :created } #in the browser url would be:- localhost:3000/company_division_stats.json
end
end
In the view:-
var chartData = <%= @all_companies.to_json.html_safe %>
Upvotes: 0
Reputation: 118261
I need to convert my output array to the specific graph format.
Do this using Awesome Print
Rubygem -
require "awesome_print"
hsh = [{:Country=>"Samsung", :Visits=>8}, {:Country=>"Natraj", :Visits=>2},
{:Country=>"Tupperware", :Visits=>5}, {:Country=>"Transcen", :Visits=>0},
{:Country=>"camlin", :Visits=>0}]
ap hsh,{:index => false}
output
[
{
:Country => "Samsung",
:Visits => 8
},
{
:Country => "Natraj",
:Visits => 2
},
{
:Country => "Tupperware",
:Visits => 5
},
{
:Country => "Transcen",
:Visits => 0
},
{
:Country => "camlin",
:Visits => 0
}
]
Upvotes: 1
Reputation: 15010
Because you are not printing your value. The console is only displaying it, double quotes are escaped.
Otherwise how would you know where my string: "my "string" what" ends?
If you just do
puts @specific_details.to_s
you will not see those double quotes escaped.
Upvotes: 1