This is a Sinatra app using a Sprockets Rack endpoint. When I reference a JavaScript file from an HTML page, I'd like to use a relative path to point to its location instead of just including the script file into the HTML. Doing so would enhance debugging, at the very least.
The Sprockets Rack definition in config.ru is:
map '/assets' do
SPROCKETS = Sprockets::Environment.new
SPROCKETS.append_path File.join(SPROCKETS.root, '/public/assets/js')
SPROCKETS.append_path File.join(SPROCKETS.root, '/public/assets/css')
SPROCKETS.append_path File.join(SPROCKETS.root, '/public/assets/fonts')
SPROCKETS.append_path File.join(SPROCKETS.root, '/public/assets')
SPROCKETS.paths.each{|path| puts path;}
SPROCKETS.js_compressor = :uglify
SPROCKETS.css_compressor = :scss
run SPROCKETS
end
To reference a JavaScript file in HTML, I define it within a script tag that effectively includes the source as a string as follows: (this works)
<script><%= SPROCKETS["frontend.js.erb"].to_s %></script>
What I would like to do is reference the file as an independent source as follows:
<script type="text/javascript" src="<%= SPROCKETS["frontend.js.erb"].filename %>"></script>
That option returns the following message:
Not allowed to load local resource: file:///C:/Bitnami/rubystack-2.2.5-3/projects/myapp/public/assets/js/frontend.js.erb
Obviously, this occurs because the Sprockets "filename" method returns the full path and the page cannot access the local file system. What I need is a method that returns the server's relative path.
Alright, just another head-slap moment...
Should have been using the standard assets path defined right there in the config.ru that I entered, as follows:
<script type="text/javascript" src="assets/frontend.js.erb"></script>
Works great.