gpt4 book ai didi

ruby-on-rails - 使用HAML有条件地设置HTML元素ID

转载 作者:行者123 更新时间:2023-12-03 13:21:48 24 4
gpt4 key购买 nike

我正在尝试在haml中编写此html,以便在该项为当前项时添加一个id标记。这是为jquery突出显示设置的。

<% if line_item == @current_item %>
<tr class="line_item id="current_item">
<% else %>
<tr class="line_item">
<% 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>

因为我不想编写一个辅助方法,所以将if语句插入了标签中:
%tr.line_item{ :id => (line_item == @current_item ? '' : 'current_item') }
%td
= line_item.quantity
%td
\x #{line_item.product.title}
%td.item_price
= number_to_currency(line_item.total_price)
%td.item_remove
= button_to 'Remove', line_item, :method => :delete

但是,此“current_item” ID标记会粘贴所有项目,而不仅仅是当前项目。这导致javascript突出显示全部或错误的条目。关于如何获得合作的想法?

最佳答案

嗯,先生,您的情况不对:-)

它应该是

line_item == @current_item ? "current_item" : ""

有一件不太漂亮的事情-剩下的项目最终都用 id=""结束。但是有一个简单的解决方法:
%tr.lineitem{ :id => (line_item == @current_item ? "current_item" : nil)}

当您返回属性的 nil值时,HAML将忽略它,并且它不会出现在输出中。

关于ruby-on-rails - 使用HAML有条件地设置HTML元素ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3841116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com