How can I reference a javascript file with slim

nilanjan picture nilanjan · Jan 4, 2014 · Viewed 9.3k times · Source

I understand how to embed javascript using javascript: in a slim template. Is there a way for me to reference a javascript file.

get '/about' do
  @title = "All About This Website"
  slim :about
end

here is about.slim

p This site is a demonstration of how to build a website using Sinatra.
javascript:
  alert("hello world")

that works. Can I reference a javascript file instead of the javascript statment/s? like this:

p This site is a demonstration of how to build a website using Sinatra.
javascript:
  about.js

Answer

Dineshkumar picture Dineshkumar · Jun 7, 2015

I've used script tag to refer the file within the slim template and it works for me.

script src="jsfile.js"

The only thing we need to consider is, by default sinatra looks for static files in public folder, so keep the js file in the public folder. Try to access your js file from your browser. All files available under public directory should be accessible (without public in url).

yourhostedsite:port/jsfile.js

Your sinatra project structure might look like

Project root
    |___ controller.rb
    |___ public
    |       |___ jsFile.js
    |       |___ cssFile.css
    |
    |___ views
            |___ html.slim