Rails helper - 标记之间的换行符

时间:2012-05-26 15:51:23

标签: ruby-on-rails tags view-helpers line-breaks

帮助器中的代码:

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。

1 个答案:

答案 0 :(得分: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
相关问题