Question

I'm using Haml in rails and have been writting with the :markdown filter (bluecloth gem), but a piece of example code is in ruby and the page tries to execute the #{values}, how can I stop this?

Here is the breaking bit of code:

: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

The error that is returned:

undefined local variable or method `f'

I have found a solution myself but it's far from ideal:

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

I place this before the :markdown filter begins.

I know it can be done because stackoverflow didn't break when I wrote that, so how can I achieve this too?

Thanks in advance!

Was it helpful?

Solution

Per the Haml documentation, interpolation within filters is normal and expected. To circumvent it, you can just escape the #{} interpolation syntax by prepending a backslash:

: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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top