ruby-on-rails – Rails html.erb文件类型,<%%>和<%=%>之间的差异

参见英文答案 > What is the difference between <%, <%=, <%# and -%> in ERB in Rails?                                    7个
我刚开始深入研究Ruby和Ruby on Rails,我注意到在.html.erb文件中嵌入Ruby语法的两个选项:

<% #ruby code here %>

要么:

<%= #ruby code here %>

我应该如何选择其中一个?

最佳答案
<%=输出Ruby的结果. <%just评估Ruby.

<p>Hi! How are you? 1 + 1 = <%= 1 + 1 %></p>

将成为< p>嗨!你好吗? 1 1 = 2< / p>.

<p>Hi! How are you? 1 + 1 = <% 1 + 1 %></p>

将成为< p>嗨!你好吗? 1 1 =< / p>.

<%通常用于流量控制,例如,如果别的.例:

<% if model.nil? %>
  <%= render 'new_model_form' %>
<% else %>
  <%= render 'detail_view' %>
<% end %>

阅读更多http://guides.rubyonrails.org/layouts_and_rendering.html

转载注明原文:ruby-on-rails – Rails html.erb文件类型,<%%>和<%=%>之间的差异 - 代码日志