2016-11-30 3 views
0

私は法ヘルパーメソッドヘルパーで条件付きクラスを持つ方法は?

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 

で条件クラスを持っていたしたいと思います。しかし、私はこのエラーを取得する:あなたはヘルパーである

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

答えて

0

を、だから使用することはできません<%= ... %>しかし、使用する必要があります#{....}

あなたのヘルパーは、これを返す必要があります:

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 
+0

ありがとう、私は完全にこの詳細を忘れて!しかし、#{...}でさえ、ビューでhtmlと解釈されないのはなぜですか? – Orsay

+0

'.html_safe'を使用していますか? – Fallenhero

+0

はい、でも.html_safe。

Orsay