1

I'm currently trying to integrate d3 with my rails app. After installing the d3-rails gem, I came across this tutorial on d3. Somewhere in the tutorial, the below code is provided

<meta charset="utf-8">
<style>

.chart div {
  font: 10px sans-serif;
  background-color: steelblue;
  text-align: right;
  padding: 3px;
  margin: 1px;
  color: white;
}

</style>
<div class="chart"></div>
<script>

var data = [4, 8, 15, 16, 23, 42];

var x = d3.scale.linear()
    .domain([0, d3.max(data)])
    .range([0, 420]);

d3.select(".chart")
  .selectAll("div")
    .data(data)
  .enter().append("div")
    .style("width", function(d) { return x(d) + "px"; })
    .text(function(d) { return d; });

</script>

If I were to include the above code as it is into my new.html.erb file, the chart will show.

I then created a new graph.js fiel under app/assets/javascripts and decided to put everything between the <script> </script> tag inside graph.js. Now when I open up new.html.erb, the chart will not show anymore.

Am I doing something wrong here that's causing rails to not recognize graph.js ?

1
  • I think you should do two things: open the source code of the page, look into /scripts/application.js and examine if d3 code has been included. Second thing is to open the developer console of the browser and examine for potential exceptions with JavaScript code. Commented Nov 22, 2015 at 22:28

1 Answer 1

1

Did you compile the assets?

You'll need to run:

rake asset:precompile
Sign up to request clarification or add additional context in comments.

2 Comments

rails is giving a Don't know how to build task 'asset:precompile' error. Not exactly sure what it means
I think it's suppose to be rake assets:precompile , but I tried that and it still does not resolve my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.