I have javascripts in the Asset Pipeline that interact with specific DOM elements on a specific view. For example:
# app/assets/javascripts/book.js
...
var svg = d3.select("#book").append("svg")
...
My view /pages/book
looks like:
<% content_for :head do %>
<%= javascript_include_tag 'd3' %>
<% end %>
<h1>Book</h1>
<div id="book"></div>
<% content_for :body do %>
<%= javascript_include_tag 'book' %>
<% end %>
My assets.rb
has:
Rails.application.config.assets.precompile += %w( d3.js )
Rails.application.config.assets.precompile += %w( book.js )
When I go to localhost:3000/pages/book
it works perfectly, the SVG
binds to the #book
When I start at root localhost:3000
and follow a link <%= link_to "See Book", pages_book_path %>
, the DOM elements exist, the HTML
renders, but the SVG
does not bind to #book
.
Looking in the terminal, when I go directly to localhost:3000/pages/book
the Asset Pipeline is loaded and the javascript executes correctly. And when I start at root
, the same thing happens, but since #book
does not exist on the root
view, nothing happens...and when I follow the link, the asset pipeline does not reload...
How can I get the asset pipeline to reload when the link is followed?