Passing string form Ruby to JavaScript: escape (single) quotes first!

Ok, this one is gonna be a short one. Short but tricky. Pay attention to repeated backslashes(four of them) in replace pattern:

"string containing 'single quotes'".gsub(/'/, "\\'")

Or, if you are on Rails, you can use one of its helpers: escape_javascript