0

I have a bunch of Javascripts in a folder under /public/javascripts/test and I want to include them in my layout using javascript_include_tag in as few lines as possible.

Currently I have 25 lines of code using javascript_include_tag, is there any way to do this better? Also I cannot use javascript_include_tag :all :recursive since there are some JS files in other directories that I do not want included.

Thanks for the help.

UPDATE: So now I have a javascript.rb initializer that has this single line of code (very long):

ActionView::Helpers::AssetTagHelper.register_javascript_expansion :syntax_highlight => Dir["#{Rails.root.to_s}/public/javascripts/sh/*.js"].each {|js| js.gsub!("#{Rails.root.to_s}/public/javascripts/",'')}

Now in my layout I have the follwing:

<%= javascript_include_tag :syntax_highlight %>

Now when I render the page I get the following:

<script src="/javascripts/sh/shAutoloader.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushAppleScript.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushAS3.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushBash.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushColdFusion.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushCpp.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushCSharp.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushCss.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushDelphi.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushDiff.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushErlang.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushGroovy.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushJava.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushJavaFX.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushJScript.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushPerl.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushPhp.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushPlain.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushPowerShell.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushPython.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushRuby.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushSass.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushScala.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushSql.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushVb.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shBrushXml.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shCore.js?1278089940" type="text/javascript"></script> 
<script src="/javascripts/sh/shLegacy.js?1278089940" type="text/javascript"></script> 

But for some reason none of this works? What gives?

3 Answers 3

1

Probably there's even better solution for that, but this will still do the trick:

Dir["#{Rails.root.to_s}/public/javascripts/test/*.js"].each {|js_file| javascript_include_tag js_file }
Sign up to request clarification or add additional context in comments.

1 Comment

For some reason this does not work... It causes the page to render really weird - the string of javascripts ends up within <body>
1

try this

In config/application.rb

uncomment this line

config.action_view.javascript_expansions[:defaults] = %w(javascript1.js javascript2.js)

and add the names of the files, like javascrip1.js and javascrip2t.js and so on.

Then in views/layouts add this line

<%= javascript_include_tag :defaults %>

Vlad Khomich's answer is also great and will work.

Comments

1

Considering that you are including so many js files, I am assuming, that you want to include all the js files under public directory. If this is the case, you can change in your javascript_include_tag as following, it will help you serve the whole file as one in production, saving lots of requests:

javascript_include_tag :all, :recursive => true, :cache => true

If you want to treat the test files saperately, you can register_javascript_expansion, like :defaults, and also use the code from Vlad Khomich's answer.

# config/init/javascript.rb
ActionView::Helpers::AssetTagHelper.register_javascript_expansion :tests => [array_of_js_paths_from_previous_answer]

# application.html.erb
<%= javascript_include_tag :tests, :cache => :tests %>

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.