Trip
Trip

Reputation: 27114

Why doesn't the shorthand escape_javascript actually work?

Here's my biopsis

Works :

:plain
  console.log("#{escape_javascript(render(@job_charge).html_safe)}");

Does not work ( but should? ) :

:plain
  console.log("#{j render(@job_charge).html_safe }");

Upvotes: 1

Views: 595

Answers (3)

sandre89
sandre89

Reputation: 5908

j is, indeed, shorthand for escape_javascript according to the docs: http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-j

I use it all the time and it works.

Upvotes: 0

Michael Slade
Michael Slade

Reputation: 13877

j is shorthand for json_escape.

escape_javascript also exists (at least in rails 3.x) but does something different - it escapes strings for use inside javascript string literals.

I guess the first sample is failing for a different reason. Check your log!

Upvotes: 1

tsherif
tsherif

Reputation: 11710

It looks like j is actually shorthand for json_escape, not escape_javascript.

http://api.rubyonrails.org/classes/ERB/Util.html

Upvotes: 4

Related Questions