HAML-Ruby-条件句

HAML-Ruby-条件句,ruby,haml,erb,Ruby,Haml,Erb,我正在阅读《Rails环境下的敏捷Web开发》一书中的示例,但将其与我发现有用的其他技术(如haml)混合使用。 有一个棘手的问题,如何写下此erb部分: <% if line_item == @current_item %> <tr id="current_item"> <% else %> <tr> <% end %> <td><%= line_item.quantity %>&times;<

我正在阅读《Rails环境下的敏捷Web开发》一书中的示例,但将其与我发现有用的其他技术(如haml)混合使用。 有一个棘手的问题,如何写下此erb部分:

<% if line_item == @current_item %>
<tr id="current_item">
<% else %>
<tr>
<% end %>
  <td><%= line_item.quantity %>&times;</td>
  <td><%= line_item.product.title %></td>
  <td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
</tr>

但是它打印出一个空的TR,其中没有TD在…

之内,而不是有两个单独的
%TR
条目(在这种情况下,我认为您需要在每个TR下列出您的3个TD),您可以只在条件中设置id:

%tr{:id => (line_item == @current_item) ? "current_item" : false}
  %td!=line_item.quantity.to_s+"&times;"
  %td=line_item.product.title
  %td.item_price=number_to_currency(line_item.total_price)

与其有两个单独的
%tr
条目(在这种情况下,我认为您需要在每个tr下列出您的3个td),您只需在条件中设置id:

%tr{:id => (line_item == @current_item) ? "current_item" : false}
  %td!=line_item.quantity.to_s+"&times;"
  %td=line_item.product.title
  %td.item_price=number_to_currency(line_item.total_price)

当缩进降低一个级别时,TR标签将被关闭。没错,但是我必须降低一个级别的缩进来结束if语句…正确,正如所写的,你会;虽然Dylan的回答是正确的,但在我看来,这可能属于部分。当缩进下降一级时,TR标记将关闭。没错,但我必须下降一级缩进以结束if语句…正确,正如所写,你会;虽然Dylan的答案是正确的,但在我看来,这可能属于部分。如果Haml属性哈希中的一个值为false,则该属性将被完全忽略,因此您可以执行
(line\u item==@current\u item)?“current_item”:false
,它将更接近原始erb。如果Haml属性哈希中的值为false,则该属性将被完全忽略,因此您可以执行
(line_item==@current_item)?“当前项目”:false
,它将更接近原始雇员再培训局。