views:

36

answers:

1

I am attempting to write this html in haml so as to add an id tag if the item is the current one. This is to setup for jquery highlighting.

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

Because I don't want to write a helper method, I've stuck the if statement within the tag:

%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

However, this id tag of 'current_item' sticks with all the items and not just the current one. This results in javascript highlighting all or the wrong entry. Thoughts on how to get haml to cooperate?

+2  A: 

Ehm sir, your condition is wrong :-)

It should be

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

There is a thing that's not pretty - you end up with id="" for rest of the items. But there is a simple cure for it:

%tr.lineitem{ :id => (line_item == @current_item ? "current_item" : nil)}

When you return nil value for an attribute HAML will ignore it and it will not appear in the output.

pawien
Thanks! Wish I could make it cleaner
ScotterC