The contents of the filter are not interpreted as Haml. You can use #{...} for interpolation, though, and thatβs why you see an error - the filter sees #{number} in "#form_#{number}" , but the line above where number defined is simply passed as is, rather than being treated as Ruby, since Ruby is concerned about number still undefined.
In this case, you can do something like:
:javascript #{[1,2,3].map do |number| "$(\"#form_#{number}\").my_method();" end.join("\n")}
although it's a little cumbersome.
A clearer solution could be to create a helper method for creating javascript, which you could call from a filter:
def create_js(arr) arr.map do |number| "$(\"#form_#{number}\").my_method();" end.join("\n") end
and Haml will be
:javascript #{create_js([1,2,3])}
source share