I have some html pages in which each page includes some javascripts files. Where I have to place this files?
folder/
page1/
index.html
script1.js
page2/
index.html
script2.js
Updated
I explain better. I have some little projects posed by an html page and some javascripts files, in which i use a canvas. I have different scripts for each page.
/sketches
/project1
index.html
script1.js
sketch.js
/project2
index.html
sketch.js
The files named 'sketch' are different between them. I tought to make one controller with a parameter id and basing on this I'll load a different project. I did it putting the html page inside the public folder and it worked, but I have problems with the javascripts, that are refused.
So, for what you are saying to me, I have to place all the javascripts in a separate folder... I'd like to keep that structure. Is there no way in rails to serve that folder, which contains all the projects, as static?
I have some html pages in which each page includes some javascripts files. Where I have to place this files?
folder/
page1/
index.html
script1.js
page2/
index.html
script2.js
Updated
I explain better. I have some little projects posed by an html page and some javascripts files, in which i use a canvas. I have different scripts for each page.
/sketches
/project1
index.html
script1.js
sketch.js
/project2
index.html
sketch.js
The files named 'sketch' are different between them. I tought to make one controller with a parameter id and basing on this I'll load a different project. I did it putting the html page inside the public folder and it worked, but I have problems with the javascripts, that are refused.
So, for what you are saying to me, I have to place all the javascripts in a separate folder... I'd like to keep that structure. Is there no way in rails to serve that folder, which contains all the projects, as static?
Share Improve this question edited Dec 27, 2017 at 12:36 Giuseppe asked Dec 27, 2017 at 8:57 GiuseppeGiuseppe 714 silver badges12 bronze badges 1- Please accept answer if it is helpful to you. – Vishal Commented Dec 27, 2017 at 9:53
4 Answers
Reset to default 6You have to place all js files under app/assets/javascripts
folder.
Updated Answer
Load the main JavaScript in application.js every time. Now create files for different needs. Create a form.js file, a myfancypart.js file etc. Don't load them in the application.html.erb layout. Load them dynamically when you need them:
application.html.erb:
<%= javascript_include_tag "application" %>
<%= yield :javascript_includes %>
top of your view.html.erb OR users/new.html.erb:
<% content_for :javascript_includes do %>
<%= javascript_include_tag "users_new.js" %>
<% end %>
Everything in the content_for block will be loaded at yield :javascript_includes.
Source: stackoverflow
The best practice is to place all your third party assets in vendor/assets
folder and your app specific assets in app/assets
folder. And you should require
the assets in their specific manifest files.
If your vendor/assets/javascripts
folder have some js files like this,
`/vendor/
/assets/
/javascripts/`
script1.js
script2.js
Then in your app/assets/javascripts/application.js
just add
//= require script1
//= require script2
In rails 5.1+, the assets
directory is removed from vendor, so you should create your own directories and either add it to config/initializers/assets.rb
to prepile or use relative path to the file with respect to vendor directory.
You should follow like this:
folder/
pages/
index.html
aboutus.html
assets/
js/
some_js.js
anoter_js.js
css/
homepage.css
about.css
/sketches
sketch.js
/project1
index.html <-- include relative path for sketch.js
script1.js
/project2
index.html <-- include relative path for sketch.js
I think u can just include relative path js file from your parent folder so u don't need to put to every subfolders.