9

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

4 Answers 4

7

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")]
Sign up to request clarification or add additional context in comments.

2 Comments

Method you have provided work with mistake. It create absolute urls to files. Here modified version that works: def collect_javascript_files(path) Dir[config.javascripts_dir + File::SEPARATOR + path].map { |s| s.sub(config.assets_dir, "") } end
how do you get this to work? I put helper_method :collect_js_files and your function in application_controller.rb, but when i call =javascript_include_tag collect_js_files('public/javascripts/folder') in my html.haml it does nothing (empty line in the generated html) this would be really useful if it works, thanks!
6

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

# < FolderWithFiles >/activate.js :

//= require_tree . 

Comments

1

The following (unoptimized) code should work in rails 3.0 or above

<%= javascript_include_tag Dir[Rails.root.join("public/javascripts/views/**/*.js")].map { |s| s.sub(Rails.root.join("public/javascripts/").to_s, "") } %>

1 Comment

This works perfectly for me. Thanks a lot. My .js files are present under the folder javascripts/controls with you solution it is able to add all files under this folder
0

You can use

<%= javascript_include_tag :all %>

But this will work only if the scripts are stored in your

RAILS_ROOT/public/javascripts

directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.