开发者

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.j开发者_开发百科s' %>
..

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


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.


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


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.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜