I have an library of scripts I would like to implement on the client side of my rails application;
In the View, I know that I can do the following.
<%= javascript_include_tag 'folder/script.js' %>
..
And because there are so many separate script files, this process is very time wasting and redundant.
I know that there's a technique to include all scripts in the public/javascript folder
<%= javascript_include_tag :all %>
But this would include the unwanted scripts outside of the destination folder specifically for the one View.
Is there a way to only include all scripts in an specific folder?
Thanks in advance
It is built right into Rails. Using the accepted answer is poor style. Please use the following:
<%= javascript_include_tag :all, :recursive => true %>
Using packagers is great for production. But this is the right way to do it in a simple, lightweight, development environment.
You can use the standard javascript_include_tag along with the Dir.glob method to include all the files in a directory. So, something like this should work:
<%= javascript_include_tag Dir.chdir(File.join(Rails.root, "public", "javascripts", "your", "subdiretory")) { |d| Dir.glob("*.js") } %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With