Tim Sullivan
Tim Sullivan

Reputation: 16888

Haml: how do I set a dynamic class value?

I have the following html.erb code that I'm looking to move to Haml:

<span class="<%= item.dashboardstatus.cssclass %>" ><%= item.dashboardstatus.status %></span>

What it does is associate the CSS class of the currently assigned status to the span.

How is this done in Haml? I'm sure I'm missing something really simple.

Upvotes: 20

Views: 7244

Answers (3)

Cristian Rennella
Cristian Rennella

Reputation: 727

You can do multiple conditional class selectors with array syntax:

%div{ class: [ ("active" if @thing.active?), ("highlight" if @thing.important?) ] }

Upvotes: 1

joeyk16
joeyk16

Reputation: 1415

This worked.

Where ever the link is to the page do something like this

  %div{"data-turbolinks" => "false"}
    = link_to 'Send payment', new_payments_manager_path(sender_id: current_user.id, receiver_id: @collaboration.with(current_user).id, collaboration_id: params[:id]), class: 'button'

Upvotes: 0

Christian Lescuyer
Christian Lescuyer

Reputation: 19263

Not sure. Maybe:

%span{:class => item.dashboardstatus.cssclass }= item.dashboardstatus.status

Upvotes: 21

Related Questions