2013-02-25 64 views
2

當我做這樣的事情:Rails的渲染字​​符串的空格在HTML文件中

<%= c.title %> 

Rails的渲染這樣的:

" 
     Title 
    " 

,如果我換行字符串此行爲是不一樣的與標籤。有人能解釋一下爲什麼Rails會這樣嗎?

UPDATE

Github's Repo link

+0

你確定嗎? ''<%= c.title%>''的輸出是什麼,或者是<%=「'#{c.title}'」%>'? – Casper 2013-02-25 13:02:20

+0

前者只是「標題」,後者則是「標題」。糾正我,如果我錯了。但那爲什麼waldyr在引號內得到結果呢? – 2013-02-25 13:06:48

+3

你在其他視圖文件中有空格。例如,如果這是一個視圖模板,那麼您可能有一個包含您所看到的空白的佈局文件。您在完全脫離背景的情況下顯示的代碼完全按照預期運行。 – deefour 2013-02-25 13:15:47

回答

1

這與ERB而不是Rails有關。處理模板時,將輸出ERB標記(<%= %>)中的任何值輸出到輸出緩衝區,並簡單地執行任何其他代碼。

其他一切都保持不變。 ERB標籤之前和之後的空白空間不變。你可以做的最多的就是在標籤中使用-來讓它去掉一些空白。

<!-- --> 
<% for i in 1..10 %> 
    <%= i -%> 
<% end %> 
<!-- --> 

將輸出

<!-- foo --> 
    1 2 3 4 5 6 7 8 9 10<!-- foo --> 

注意兩個空格,每個號碼之前仍然顯示,採取的是空白,它看起來像一個長的數字。

比較這與

<% for i in 1..10 %> 
    <%= i %> 
<% end %> 

將輸出

<!-- --> 
    1 
    2 
    3 
    4 
    5 
    6 
    7 
    8 
    9 
    10 
<!-- --> 

下面是測試頁的輸出後,選擇查看源代碼從上下文菜單。

<!DOCTYPE html> 
<html> 
<head> 
    <title>Testapp</title> 
    <link href="/assets/application-1b13569e9620782f423d4cd3ce931750.css" media="all" rel="stylesheet" type="text/css" /> 
    <script src="/assets/application-9a717ea62eac3463d689b2ba0a4e85b4.js" type="text/javascript"></script> 
    <meta content="authenticity_token" name="csrf-param" /> 
<meta content="/hfgtJZWzxaQ2d7txQMAt2b+21MWSTYcf6/2F7Pei1k=" name="csrf-token" /> 
</head> 
<body> 

<h1>Home#index</h1> 
<p>Find me in app/views/home/index.html.erb</p> 
What&#x27;s wrong with me? 

<!-- -->What&#x27;s wrong with me surrounded by html commet<!-- --> 


</body> 
</html> 
+0

你做了哪個瀏覽器? – 2013-02-25 13:54:17

+0

這是來自rails應用程序的輸出html源代碼,與瀏覽器無關。 – Cluster 2013-02-25 15:16:37

+0

等一下,你在查看Chrome的Elements選項卡嗎?這與輸出源無關。你可以去掉所有的空格和換行符,並且仍然可以用chrome的方式進行格式化。右鍵單擊頁面和查看源代碼,或在命令行上使用wget/GET命令。 – Cluster 2013-02-25 15:19:43

0

你可能在你的佈局文件中的一些缺口或呈現這種觀點的另一個文件。

你application.html.erb可能看起來像這樣:

<html> 
    <head>...</head> 
    <body> 
    <%= yield %> <!-- Some whitespaces at the beginning of this line --> 
    </body> 
</html> 

的Rails簡單地表達其中的價值,僅此而已替換<%=%>標籤。

0

我沒有看到問題。這裏的渲染HTML:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Testapp</title> 
    <link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" /> 
<link href="/assets/home.css?body=1" media="all" rel="stylesheet" type="text/css" /> 
    <script src="/assets/jquery.js?body=1" type="text/javascript"></script> 
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script> 
<script src="/assets/home.js?body=1" type="text/javascript"></script> 
<script src="/assets/application.js?body=1" type="text/javascript"></script> 
    <meta content="authenticity_token" name="csrf-param" /> 
<meta content="cOfMPm5S/tCHCEHkeRTeQTITAiz800s+3Q4ZgNWCNlY=" name="csrf-token" /> 
</head> 
<body> 

<h1>Home#index</h1> 
<p>Find me in app/views/home/index.html.erb</p> 
What&#x27;s wrong with me? 


</body> 
</html> 

相關的佈局部分:

<body> 

<%= yield %> 

</body> 
+0

我剛剛注意到,只有Chrome有這個問題 – 2013-02-25 13:47:04

+0

@ waldyr.ar這就是我正在使用的。 – 2013-02-25 13:54:27

+0

你可以請檢查這個[鏈接](http://nameless-inlet-3870.herokuapp.com/)並告訴我你使用哪個chrome版本? – 2013-02-25 13:59:48

0

我與你有同樣的問題!

對我來說,解決方案是從UTF-8文件編碼改爲UTF-8 without BOM

我使用西納特拉,很高興與大家分享!