Orsay Orsay - 9 days ago 4
HTML Question

how to had conditionnal class in a method helper?

I'd like to had a conditionnal class in a method helper

product_helper.rb

def product_icon
...
elsif product.sent?
'<div class="green-text <%= 'middle' if current_action?(controller: 'products', action: 'index')" %> ><i class="fa fa-envelope-o" aria-hidden="true"></i></div>'.html_safe
end


But I get this ERROR :

syntax error, unexpected keyword_class, expecting keyword_end
'<div <%= 'class="middle if current_acti...
^


Any help please ? I can't find the solution

Answer

You are in the helper, so you cannot use <%= ... %> but need to use #{....}

Your helper should return this:

return "<div class='green-text <%= 'middle' if current_action?(controller: 'products', action: 'index')'%>><i class='fa fa-envelope-o' aria-hidden='true'></i></div>".html_safe