【问题标题】:Rails ApplicationHelper Escaping HTML without return statementRails ApplicationHelper 在没有返回语句的情况下转义 HTML
【发布时间】:2011-02-11 21:46:51
【问题描述】:

当我写作时

module ApplicationHelper   
  def flash_helper 
    flash.each do |key, msg|
      content_tag :div, msg, :class => key
      ## "<div class=\"key\">" + msg + "</div>"
    end
  end
end

除非我return 声明,否则我什么也得不到。当我调用&lt;%= flash_helper %&gt; 时,HTML 在我看来是转义的。是什么赋予了?如何防止 HTML 被转义?

【问题讨论】:

    标签: html ruby-on-rails helpers actionview escaping


    【解决方案1】:

    你可以这样重写吗?

    module ApplicationHelper   
      def flash_helper 
        s = ""
        flash.each do |key, msg|
          s += content_tag :div, msg, :class => key
          ## "<div class=\"key\">" + msg + "</div>"
        end
        return s
      end
    end
    

    【讨论】:

    • 我是这样写的,遇到了和OP一样的问题。
    【解决方案2】:

    您可以使用concat 方法(Rails >= 2.2.1)

    module ApplicationHelper   
      def flash_helper 
        flash.each do |key, msg|
          concat(content_tag :div, msg, :class => key)
        end
        nil
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 2015-09-12
      • 2016-02-01
      • 2019-10-12
      • 2023-02-07
      • 1970-01-01
      相关资源
      最近更新 更多