Haml: How to add classes dynamically to an element?

11,103

Solution 1

or simply:

%tr{ :class => classes }

Solution 2

Say you have an array classes = ['pck1', 'pck3', 'pck5'] with the classes you want your tr to have.

Just type something like:

%tr{:class => classes.join(' ')}
Share:
11,103
denisjacquemin
Author by

denisjacquemin

Updated on July 28, 2022

Comments

  • denisjacquemin
    denisjacquemin almost 2 years

    I have a <tr> element in my view, and I want to add classes dynamically on this element depending on an association between two models (many to many between company and packaging).

    The result should looks like

    <tr class="pck1 pck3 pck5">
    

    where pck1, pck3 and pck5 are packagings associated with a company.