4

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

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

3

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") } %>

Comments

1

http://github.com/sbecker/asset_packager

This bundles all of your javascripts into one file in production and lists them separately in development. With one rake task you define all your javascripts and stylesheets, and the order you want them stored. Another task packages them into two files. Really cool.

1 Comment

Asset packager (and others like Jammit) will also strip comments, minify your source etc. Definitely the way to go for an app in production. In development your scripts are loaded as usual.
0

For anyone using Rails 3.1 or newer, the asset pipeline is the way to go.

Comments

0

Following works 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

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.