admin管理员组文章数量:1355574
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.
本文标签: javascriptRails include js in html public filesStack Overflow
版权声明:本文标题:javascript - Rails include js in html public files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744027903a2578372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论