2012-05-26 68 views
0

之间打破了帮手此代码:Rails的帮手 - 线标签

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

你必须使用双引号"

使用"\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 
+0

谢谢,尤其是对于这个例子! – Paul