I have been exploring the gem gon in order to generate JSON data from my Rails database. I have successfully managed to display this data in an alert, but now I am looking to visualize it using d3.js.
Within my database named "users" with columns (name:string, value:integer), I only want to pass the values to JSON for graphing purposes.
I am debating whether it is more efficient to handle this task within the controller or to pass the entire table into JSON and then filter out the specific parts needed for my graph in d3.
Attempting the former approach through the following code snippet:
class GraphController < ApplicationController
def index
end
def data
gon.users = @users.value.as_json
@users = User.value
end
end
Unfortunately, executing the above code results in the error:
NoMethodError in GraphController#data
undefined method `value' for nil:NilClass
The line highlighted as problematic is:
gon.users = @users.value.as_json
I am currently unsure of how to proceed in order to graph the desired values.
UPDATE
Following a suggestion, I have decided to abandon gon and revert back to generating JSON in the traditional way. I am now attempting to retrieve this data via an AJAX call.
def data
render :json => User.select('value')
end
I am making the exact same call to the data function using AJAX as outlined in the tutorial found here.
Despite these changes, I am still encountering issues as the bar graph I desire is not being displayed and instead, I am receiving a hash of IDs and values in JSON format.