2013-10-31 47 views
3

當我在erb-模板文件中使用if語句呈現,if語句評估延遲,這是混合了HTML:Rails的模板渲染ERB if語句不是爲了

<small> 
<% if @applause_count == 1 %> 
    The author has been cheered up once! 
<% elsif @applause_count > 1%> 
    The author has been cheered up <%= @applause_count %> times! <br/>Be the next! 
<% end if %> 
</small> 

產生:

<small> 
</small> 
The author has been cheered up 100 times! <br/>Be the next! 

有人能解釋我這種奇怪的行爲嗎?

+4

我很驚訝這在所有工作,應該有一個語法錯誤'<% end if %>'(它應該是'<% end %>') –

回答

2

如前所述,問題出在<% end if %>

使用<% end %>

這將產生所需的HTML:

<small> 
The author has been cheered up 2 times! <br/>Be the next! 
</small>