Why doesn’t this work?
:javascript
-[1,2,3].each do |number|
$("#form_#{number}").my_method();
Rails gives me an error, saying that the variable number isn’t defined.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The content of a filter isn’t interpreted as Haml. You can use
#{...}for interpolation though, and that’s why you’re seeing the error – the filter sees the#{number}in"#form_#{number}", but the line above wherenumberis defined is simply passed through as it is, not treated as Ruby, so as far as Ruby is concernednumberis still undefined.In this case you could do something like:
although that’s a bit unwieldy.
A clearer solution might be to create a helper method to create the javascript, which you could call from the filter:
and the Haml would be