I'm fairly new to Ruby on Rails and am quite开发者_如何学JAVA impressed with the capabilities built-in. At some point while looking at the HTML that my Rails app was generating it occurred to me that some of the indenting wasn't perfect. Seeing as Ruby is so visually appealing, why shouldn't I expect the markup it creates to be?
So, I've got the following:
<%= javascript_include_tag :defaults %>
which generates the following:
<script src="/javascripts/prototype.js?1295468839" type="text/javascript"></script>
<script src="/javascripts/effects.js?1295468839" type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1295468839" type="text/javascript"></script>
<script src="/javascripts/controls.js?1295468839" type="text/javascript"></script>
<script src="/javascripts/rails.js?1295468839" type="text/javascript"></script>
<script src="/javascripts/application.js?1295468839" type="text/javascript"></script>
Notice the first line has the appropriate indentation of two spaces while each subsequent line is placed at the very beginning.
I know that I'm being a bit picky but it's 2011. Our markup should be as pretty as our UI.
Is there a way to set a new-line "delimiter" or otherwise tell rails to indent when using the AssetTagHelper?
There's no option to do this for javascript_include_tag. It just joins all the sources with newlines.
You could manually emit each script tag with something like:
<%= javascript_include_tag "prototype" %>
<%= javascript_include_tag "effects" %>
<%= javascript_include_tag "dragdrop" %>
<%= javascript_include_tag "controls" %>
<%= javascript_include_tag "rails" %>
<%= javascript_include_tag "application" %>
Another option (and a better one IMO), is to use an asset packager like Jammit - http://documentcloud.github.com/jammit/ to concatenate and minify the JS into a single file.
精彩评论