0

助手中的这段代码:

def dgtags
    if params[:controller]=='my_controller'
      javascript_include_tag('dygraph-combined.js') <<
          tag(:meta, :http_equiv => 'X-UA-Compatible', :content => 'IE=EmulateIE7; IE=EmulateIE9') <<
          '<!--[if IE]>'.html_safe <<
          javascript_include_tag('excanvas.compiled.js') <<
          '<![endif]-->'.html_safe
    end
end

产生以下输出:

<script src="/javascripts/dygraph-combined.js?1338036501" type="text/javascript"></script><meta content="IE=EmulateIE7; IE=EmulateIE9" http_equiv="X-UA-Compatible" /><!--[if IE]><script src="/javascripts/excanvas.compiled.js?1237712986" type="text/javascript"></script><![endif]-->

如何在标签之间插入换行符?像这样:

<script src="/javascripts/dygraph-combined.js?1338036501" type="text/javascript"></script>
<meta content="IE=EmulateIE7; IE=EmulateIE9" http_equiv="X-UA-Compatible" />
<!--[if IE]><script src="/javascripts/excanvas.compiled.js?1237712986" type="text/javascript"></script><![endif]-->

'\n' 或 '\n'.html_safe 无济于事 - 它按字面意思生成 \n。

4

1 回答 1

1

你必须使用双引号 "

"\n"不使用'\n'

您可以在此处获取更多详细信息:http ://en.wikibooks.org/wiki/Ruby_Programming/Strings

我已经稍微改变了你的代码。更好的方法是使用"\n". 您也可以使用controller_name而不是params[:controller].

def dgtags
    if controller_name == 'my_controller'
      [ javascript_include_tag('dygraph-combined.js'),
        tag(:meta, :http_equiv => 'X-UA-Compatible', :content => 'IE=EmulateIE7; IE=EmulateIE9'),
        '<!--[if IE]>',
        javascript_include_tag('excanvas.compiled.js'),
        '<![endif]-->'].join("\n").html_safe
    end
end
于 2012-05-26T16:00:55.533 回答