Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include an public javascript folder in RAILS Views

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

like image 553
Dennis D Avatar asked Dec 29 '22 08:12

Dennis D


2 Answers

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.

like image 70
adu Avatar answered Dec 30 '22 21:12

adu


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") } %>
like image 35
venables Avatar answered Dec 30 '22 22:12

venables