Haml: how do I set a dynamic class value?
Asked Answered
M

3

20

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.

Mocha answered 8/8, 2008 at 19:56 Comment(1)
was your solution the same as below?Creepy
E
21

Not sure. Maybe:

%span{:class => item.dashboardstatus.cssclass }= item.dashboardstatus.status
Euxenite answered 8/8, 2008 at 20:28 Comment(1)
how do you do it when your tag is not a direct html tag, but generated through a rails helper?Vermiculate
S
1

You can do multiple conditional class selectors with array syntax:

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

Sinkage answered 14/12, 2019 at 9:40 Comment(0)
M
0

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'
Missile answered 20/9, 2017 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.