【问题标题】:Rails helper - line breaks between tagsRails 助手 - 标签之间的换行符
【发布时间】:2012-05-26 15:51:22
【问题描述】:

帮助程序中的这段代码:

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。

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 谢谢你,特别是例子!
    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多