Giuseppe
Giuseppe

Reputation: 71

Rails include js in html public files

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?

Upvotes: 0

Views: 4156

Answers (4)

Bernie Chiu
Bernie Chiu

Reputation: 263

/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.

Upvotes: 1

Vishal
Vishal

Reputation: 7361

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

Upvotes: 6

Muhammed Anees
Muhammed Anees

Reputation: 1850

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.

Upvotes: 2

breq
breq

Reputation: 25526

You should follow like this:

folder/
  pages/
    index.html
    aboutus.html
  assets/
    js/
      some_js.js
      anoter_js.js
    css/
      homepage.css
      about.css

Upvotes: 1

Related Questions