Include public javascript folder in RAILS Views

I have a library of scripts that 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 since there are so many separate script files, this process is very wasted and redundant.

I know that there is a way to include all scripts in the public / javascript folder

<%= javascript_include_tag :all %>

But this will include unwanted scripts outside the target folder specifically for one view.

Is there a way to include only all scripts in a specific folder?

Thanks in advance

+3
source share
5 answers

javascript_include_tag Dir.glob . , - :

<%= javascript_include_tag Dir.chdir(File.join(Rails.root, "public", "javascripts", "your", "subdiretory")) { |d| Dir.glob("*.js") } %>
+3

Rails. - . , :

<%= javascript_include_tag :all, :recursive => true %>

. , , .

+6

http://github.com/sbecker/asset_packager

javascripts . javascripts , . . .

+1

, Rails 3.1 , - .

0
source

Following work for me

def include_folder_javascripts(path)
    output = []
    Dir.chdir(File.join(Rails.root, path)) { |d|
      output << Dir.glob("*.js").map{|js| javascript_include_tag('calendar_date_select/'+js)}.join
    }
    raw output * "\n"
  end
0
source

Source: https://habr.com/ru/post/1760644/


All Articles