【问题标题】:Haml Markdown Filter Breaking on Example Ruby Code in Rails在 Rails 中的示例 Ruby 代码上破坏 Haml Markdown 过滤器
【发布时间】:2011-04-11 05:03:23
【问题描述】:

我在 Rails 中使用 Haml,并且一直在使用 :markdown 过滤器(bluecloth gem)编写,但是一段示例代码在 ruby​​ 中,并且页面尝试执行 #{values},我该如何阻止它?

下面是一段代码:

:markdown
  like_frags = [fields].flatten.map { |f| "LOWER(#{f}) LIKE :word#{count}" }
    or_frags << "(#{like_frags.join(" OR ")})"
    binds["word#{count}".to_sym] = "%#{word.to_s.downcase}%"
    count += 1
  end

返回的错误:

undefined local variable or method `f'

我自己找到了解决方案,但远非理想:

- f=nil;count=nil;like_frags=[];word=nil;

我把它放在 :markdown 过滤器开始之前。

我知道这是可以做到的,因为我写的时候 stackoverflow 没有中断,那么我怎样才能做到这一点呢?

提前致谢!

【问题讨论】:

    标签: ruby-on-rails ruby haml markdown bluecloth


    【解决方案1】:

    根据 Haml 文档,过滤器内的插值是正常的和预期的。为了规避它,您可以通过添加反斜杠来转义 #{} 插值语法:

    :markdown
      like_frags = [fields].flatten.map { |f| "LOWER(\#{f}) LIKE :word\#{count}" }
        or_frags << "(\#{like_frags.join(" OR ")})"
        binds["word\#{count}".to_sym] = "%\#{word.to_s.downcase}%"
        count += 1
      end
    

    【讨论】:

    • 哈,我尝试转义它们,但愚蠢地使用了正斜杠而不是反斜杠!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 2011-07-17
    相关资源
    最近更新 更多