0

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 composed 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?

Giuseppe
  • 61
  • 4
  • 12

4 Answers4

6

You 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

Vishal
  • 7,113
  • 6
  • 31
  • 61
  • The problem is that I need to distribute the javascripts files. Is it possible? I have much pages and much javascripts to include, different in each page. Is a mess for me to place all javascripts apart and include them in the pages, also beacause this javascripts files have some names equals – Giuseppe Dec 27 '17 at 11:37
  • @GiuseppeMagazzù When you generate controller in rails , it will automatic generate its js and css file, so you can add related js code to that js file for that controller's view. – Vishal Dec 27 '17 at 11:44
2

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 precompile or use relative path to the file with respect to vendor directory.

Muhammed Anees
  • 1,810
  • 1
  • 16
  • 17
1

You should follow like this:

folder/
  pages/
    index.html
    aboutus.html
  assets/
    js/
      some_js.js
      anoter_js.js
    css/
      homepage.css
      about.css
breq
  • 24,412
  • 26
  • 65
  • 106
1
/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.

Bernie Chiu
  • 263
  • 1
  • 11