Eric K
Eric K

Reputation: 395

Assign multiple css classes to a table element in Rails

I'm trying to style a table row using both cycle and a helper, like shown:

<tr class= <%= cycle("list-line-odd #{row_class(item)}", "list-line-even #{row_class(item)}")%> >

However, when I do this, the resulting HTML is:

<tr class = "list-line-odd" lowest-price>

with the return from the helper method not enclosed in the quotes, and therefore not recognized.

Here's the helper I'm using:

  def row_class(item)
    if item.highest_price > 0 and item.lowest_price > 0 and item.highest_price != item.lowest_price
      if item.current_price >= item.highest_price
        "highest-price"
      elsif item.current_price <= item.lowest_price
        "lowest-price"
      end
    end
  end

I must be missing something obvious, but I just can't figure out how to wrap both the result of cycle and the helper method return in the same set of quotes. Any help would be greatly appreciated!

Upvotes: 2

Views: 1278

Answers (1)

Tom Harrison
Tom Harrison

Reputation: 14028

There are the quotes that you want around the class definition in the HTML, and the quotes that delimit the strings called within the ruby code. The ruby inside of the <%= calls two functions that output strings (cycle, and your method row_class) which you want separated by a space. I think either of these will do what you want:

<tr class="<%= cycle('list-line-odd', 'list-line-even') + ' ' + row_class(item) %>" >

or perhaps a little prettier

<tr class="<%= "#{cycle('list-line-odd', 'list-line-even')} #{row_class(item)}" %>" >

or even %Q|| format to avoid delimiter confusion...

<tr class="<%= %Q|#{cycle('list-line-odd', 'list-line-even')} #{row_class(item)}| %>" >

resulting in something like

<tr class="list-line-odd lowest-price" >

EDIT: My original post was wrong. Need to concatenate space between the cycle and row_class method.

Upvotes: 2

Related Questions