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
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
Share
Improve this question
asked Jan 4, 2014 at 13:07
nilanjannilanjan
6793 gold badges9 silver badges18 bronze badges
3
-
1
Just include a script tag:
script src="about.js"
– Blender Commented Jan 4, 2014 at 13:27 - Can I use a file name with the javascript label? I will post another question on why I need to do that. I realized this isn't specific to slim. I have the same issue when I use haml. – nilanjan Commented Jan 5, 2014 at 4:07
- @Blender I've tried your solution but it didnt seem to work for me. I used a gem to convert my ERB files to Slim and everything seemed to work except for my javascrip files. Do you happen to have any other solutions? Thank you. – hello_its_me Commented Oct 29, 2014 at 22:30
1 Answer
Reset to default 7I'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