1

I want to use a javascript_link_tag to access a javascript file in a folder. Here is the path:

app/assets/javscripts/ntc.js

Please help me. Thanks.

2
  • Use the assets pipeline (Sprockets). guides.rubyonrails.org/asset_pipeline.html Commented Oct 5, 2017 at 0:54
  • While you could use 'Dir.glob' and loop through the files it's better to use the built in assets pipeline that can concatenate the files together for performance Commented Oct 5, 2017 at 1:02

1 Answer 1

1

Since the file exist inside assets/javascript you can add in application.html.erb as follows:

<%= javascript_include_tag "ntc" %>
# => <script src="/assets/ntc.js"></script>

if you want to specify from other folder path then u can use as below:

<%= javascript_include_tag "xmlhr", host: "localhost", protocol: "https" %>
# => <script src="https://localhost/assets/xmlhr.debug-1284139606.js"></script>

As @Max mentioned, you can use asset pipeline to include controller specific stylesheets and JavaScript files only in their respective controllers.

If you have a folder of script files and want to load all for a particular controller for example:

assets
| 
|___ javascript
        |
        |___ ntc.js
        |
        |___ ntc
              |__script1.js
              |__script2.js

Then you can require these files inside ntc.js as follows:

 //= require ntc/script1
 //= require ntc/script2

And can use

<%= javascript_include_tag params[:controller] %>

for more information refer this documentation

Sign up to request clarification or add additional context in comments.

1 Comment

That only includes a single file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.