I am learning ROR (5+ months currently) and am trying to get data from a Postgres database into Highcharts. The Postgres table has columns for :id, :name, and :pc1 - which is one-year percent change - (among others). There are five time-series [:id] in the database.
Here's the db schema:
create_table "series", force: true do |t|
t.string "acronym"
t.string "name"
t.string "current"
t.string "previous"
t.string "pc1"
t.datetime "created_at"
t.datetime "updated_at"
end
class SeriesController < ApplicationController
before_action :set_series, only: [:show, :edit, :update, :destroy]
# GET /series
# GET /series.json
def index
@series = Series.all
end
def percent
@series.each do |series|
series.name
series.pc1
end
render :json
end
<div>
<script>
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container'
},
series: [{
// I pulled local host out of here because Stackoverflow doesn't like it
var url = "";
$.getJSON(url, function(resp) {
options.series[0].name = name;
options.series[0].pc1 = pc1;
var chart = new Highcharts.Chart(options);
});
pointStart: 0,
pointInterval
}]
});
$(function () {
$('#container').highcharts({
chart: {
type: 'bar'
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: ['Apples', 'Bananas', 'Oranges']
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
});
});
</script>
</div>
class Series < ActiveRecord::Base
has_many :series
end
1. Am I correctly rendering json in the controller?
I don't think so. I would also send a status and you need to tell what json it is. So i would go like:
def percent
@series.each do |series|
series.name
series.pc1
end
render status: 200, json: @series.as_json
end
2. Should I be calling a url in the script or a file (since I'm using a localhost server)?
A url, always a url. You won't be able to access a file. And your url seems correct.
3. I suspect there are others, e.g., should the percent method be in the before action in the controller?
It depends on what you are doing. If you want to respond with the percent method generated json by calling /series
route, you need to call percent in the index method. If you want an explicit call to /percent
you just need to add it to the routes and you are good to go. Btw, you did change the routes file for this, right?
You should at least have a get available for the series controller, or if you want full CRUD, a resource should be there.
Also, I think you should keep with the convention of having the model name on the singular form, and your has_many: series, doesn't make sense to me. What are you trying to achieve there?
If a serie has_may series, then you should have a serie_id
in the model and also a belongs to.
Hope this helps somehow.