2016-11-30 58 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,即使使用'#{...}',你知道为什么吗?即使使用'.html_safe'也可以使用 – Orsay

+0

? – Fallenhero

+0

是的,即使是.html_safe。这是确切的视图渲染:

Orsay