Question

How do you include a javascript file located in vendor/assets/javascripts in a single view file (NOT in application.js) ?

Some kind of javascript_include_tag ? Please help me with syntax

I have tried the following and it doesn't work:

  • javascript_include_tag 'respond'
  • javascript_include_tag 'respond.js'
  • javascript_include_tag 'vendor/assets/javascripts/respond'
  • javascript_include_tag 'vendor/assets/javascripts/respond.js'

Thank you

Was it helpful?

Solution

<%= javascript_include_tag "your_file" %>

If you're trying to include respond.js, you could check out the respond_rails gem, which will allow you to include this in your view:

<%= respond_include_tags %>

In your views of course

OTHER TIPS

This may be an old question but for others who are looking for an answer that works for any js file, eg: vendor/assets/randomfolder/anotherfolder/custom.js, then to include custom.js using javascript_include_tag inside an individual view file, you would need to add the following line of code into assets.rb:

Rails.application.config.assets.precompile += %w( anotherfolder/custom.js )

The path here is anotherfolder/custom.js instead of randomfolder/anotherfolder/custom.js is because any folder/file inside vendor/assets/ is automatically added to rails assets pipeline path / sprokets search path. Note that deep folders are not added, thus the anotherfolder/custom.js path.

After adding that line to assets.rb, you can now use javascript_include_tag anotherfolder/custom.js inside any view file without issues.


ADDITIONALLY, if you do not want to keep typing out the subdirectory anotherfolder, then you can go to application.rb and add it into the assets pipeline/sprokets path with the following code:

config.assets.paths << Rails.root.join("vendor", "assets", "randomfolder", "anotherfolder")

After doing so, you can now use javascript_include_tag custom.js. Using this method for limited case is ok, but to include all subdirectories (including deeper ones) is not recommended.

NOTE: Please remember to restart the server so that load paths can be updated

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top