Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use javascript_include_tag to get all scripts in a folder

I'd like to use javascript_include_tag to grab all view related scripts using recursion, which a placed in public/javascripts/views.

I'm trying javascript_include_tag "/views", :recursive => true, but failed to add any script.

Regards, Alexey Zakharov

like image 332
Alexey Zakharov Avatar asked Feb 04 '23 02:02

Alexey Zakharov


2 Answers

According to the API documentation of javascript_include_tag:

Returns an html script tag for each of the sources provided.

You can pass in the filename (.js extension is optional) of javascript files that exist in your public/javascripts directory for inclusion into the current page or you can pass the full path relative to your document root.

You cannot recursively include all files in your directory. However, you always can write something like the next line:

javascript_include_tag *Dir[Rails.root.join("public/javascripts/views/**/*.js")]
like image 119
Daniel O'Hara Avatar answered Feb 05 '23 15:02

Daniel O'Hara


A simple solution is to create a new js-file ( further called activate.js ) and include the files there.

# < FolderWithFiles >/activate.js :

//= require_tree . 
like image 44
Tilman Krauß Avatar answered Feb 05 '23 16:02

Tilman Krauß