2012-05-26 67 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