【问题标题】:how to had conditional class in a method helper?如何在方法助手中有条件类?
【发布时间】:2023-03-15 02:15:01
【问题描述】:

我想在方法助手中有一个条件类

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...
                  ^

【问题讨论】:

    标签: html ruby-on-rails class ruby-on-rails-5 helper


    【解决方案1】:

    你在助手中,所以你不能使用&lt;%= ... %&gt;,但需要使用#{....}

    你的助手应该返回这个:

    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
    

    【讨论】:

    • 谢谢,我完全忘记了这个细节!但它不会在视图中解释为 html,即使使用 '#{...}' 你知道为什么吗?
    • 即使是.html_safe?
    • 是的,即使是 .html_safe。这是确切的视图渲染:
    • 你当然需要用"替换'
    猜你喜欢
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多