This is taking me hours. It would be great if someone could show me exactly how to do this..
All Im trying to do is pass a JSON object to a html.erb page with an ajax call for a d3.js graph. Most of the explanations i have found use the json file whereas my setup uses a JSON object.
My code is as below: the model (user.rb)
class User < ActiveRecord::Base
has_many :relationships
end
class User
def self.including_relationships
User.joins("INNER JOIN relationships ON users.id = relationships.user_id").select("users.name, relationships.user_id, relationships.followsid,users.value").each_with_object(Hash.new{|h, k| h[k] = []}) do |a, obj|
obj['nodes'] << a.slice('name')
obj['links'] << a.slice('user_id', 'followsid', 'value')
end
end
end
The controller: user_controller.rb
class UserController < ApplicationController
def index
render :json => User.including_relationships
end
def data
render :json => User.including_relationships
end
my routes.rb is
Rails.application.routes.draw do
get 'user/data' => 'user#data'
resources :user
end
The view (index.html.erb)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Basic HTML5 Template</title>
<link href="stylesheets/style.css" rel="stylesheet" type="text/css" media="screen" />
<script src="example.js"></script>
</head>
<body>
<script>
$.ajax({
type: 'GET',
url: 'localhost:3000/user/index',
success: function(data) {
var miserables = data
}
})
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json(miserables, function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
</script>
</script>
</body>
</html>
end
There are no errors, however all I am returned is a JSON formatted data. I would like the view to use the JSON object from
render :json => User.including_relationships
and I though that putting it in the ajax script as
$.ajax({
type: 'GET',
url: 'localhost:3000/user/index',
success: function(data) {
var miserables = data
}
})
would be the way. But it isn't. I don't seem to be successfully pasing the JSON object to the ajax I don't think.