如果Statement不按顺序呈现,则Rails模板呈现ERB

时间:2013-10-31 15:10:14

标签: ruby if-statement ruby-on-rails-4 erb template-engine

当我在erb-模板文件中使用if语句时,if语句被延迟评估,这混合了html:

<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!

有人能解释我这种奇怪的行为吗?

1 个答案:

答案 0 :(得分:2)

如上所述,问题在于<% end if %>

使用<% end %>

这会产生所需的html:

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