Wednesday, 15 February 2012

ruby on rails - Getting data from Postgres in JSON format in ROR into a Highcharts javascript -



ruby on rails - Getting data from Postgres in JSON format in ROR into a Highcharts javascript -

i learning ror (5+ months currently) , trying info postgres database highcharts. postgres table has columns :id, :name, , :pc1 - one-year percent alter - (among others). there 5 time-series [:id] in database.

here's db schema:

create_table "series", force: true |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

here's portion of series controller:

class seriescontroller < applicationcontroller before_action :set_series, only: [:show, :edit, :update, :destroy] # /series # /series.json def index @series = series.all end def percent @series.each |series| series.name series.pc1 end render :json end

i'm defining method - "percent" - because want create several different charts, e.g., 1 year-over-year percent alter (this one) previous vs. current.

here's view highcharts script:

more or less next highcharts' instructions (here: http://www.highcharts.com/docs/working-with-data/preprocessing-data-from-a-database ), pulled "data" request out of highcharts function. original highcharts demonstration code - using embedded "hard-coded" array - here: http://www.highcharts.com/docs/getting-started/your-first-chart assume i'm supposed remove series if i'm declaring variable before calling function. i'm ignoring php in illustration because should rendering series in json controller.

<div> <script> var chart = new highcharts.chart({ chart: { renderto: 'container' }, series: [{ // pulled local host out of here because stackoverflow doesn't 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>

here's model:

class series < activerecord::base has_many :series end

i think have multiple issues: 1. correctly rendering json in controller? 2. should calling url in script or file (since i'm using localhost server)? 3. suspect there others, e.g., should percent method in before action in controller?

the error in console line: var url = "h t t p:// local host (since can't utilize word):3000/series"; i've tried number of variations including /series , file path.

my environment set-up (using apple mac mavericks). utilize terminal app rails server running, postgres running in separate tab, sublime text version 2, , chrome.

thank in advance help. first question / post stackoverflow. great resource!

1. correctly rendering json in controller?

i don't think so. send status , need tell json is. go like:

def percent @series.each |series| series.name series.pc1 end render status: 200, json: @series.as_json end

2. should calling url in script or file (since i'm using localhost server)?

a url, url. won't able access file. , url seems correct.

3. suspect there others, e.g., should percent method in before action in controller?

it depends on doing. if want respond percent method generated json calling /seriesroute, need phone call percent in index method. if want explicit phone call /percent need add together routes , go. btw, did alter routes file this, right? should @ to the lowest degree have available series controller, or if want total crud, resource should there. also, think should maintain convention of having model name on singular form, , has_many: series, doesn't create sense me. trying accomplish there? if serie has_may series, should have serie_id in model , belongs to.

hope helps somehow.

javascript ruby-on-rails json postgresql highcharts

No comments:

Post a Comment