Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Conditional Classes in HAML

Using Ruby and HAML, is there a shorter way to represent this logic:

%tr{class: "#{'success' if admin.approved?} #{'warning' unless admin.approved?}"}

Thanks!

like image 440
toobulkeh Avatar asked Mar 17 '23 11:03

toobulkeh


1 Answers

You can simplify the logic using a ternary statement (one line if/else):

%tr{class: admin.approved? ? 'success' : 'warning'}

Or you could move the logic to a helper. For example, create a helper method in application_helper.rb:

def admin_row_class(admin)
  admin.approved? ? 'success' : 'warning'
end

Then use the helper in your view:

%tr{class: admin_row_class(admin)}
like image 71
infused Avatar answered Mar 28 '23 18:03

infused