Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Remove j alias for ERB::Util.json_escape #3578

merged 1 commit into from

7 participants


I noticed that this newly added j method
overwrites existing another j method
in ActionView context.

FYI: It seems this global j is called in other contexts.

Attached a patch that removes j alias for json_escape because this j is no longer available for app developers, and not used inside the framework (maybe those who are using output_safety.rb out of the Rails box would be affected... but who does?).


While I agree it is confusing having two methods called j, j is also a module function for ERB::Util. And it is common for ERB::Util have aliases based on the escape method:

It also freaks me out that some methods are called json_escape and others escape_javascript (I always get confused). It also annoys me they are in different files. Maybe the best solution would be:

1) Move escape_javascript to ERB::Util as well (or move all to AV::Helpers::EscapeHelper)
2) Alias escape_javascript to js
3) Deprecate j in all cases

But I am not sure. What do you think? /cc @jeremy


j mirrors h and is meant to only work in views. Global Kernel#j seems terrible:

+1 to consolidating the implementations, in any case.


My preference would be to consolidate in EscapeHelper, alias escape_javascript to js, and alias json_escape to j (with the much more useful definition provided in #6094).




If j is changing to js and another j is taking its place, the documentation has to be eye popping since it'll surely be confusing.

I would personally recommend leaving j aliased to escape_javascript, since it has meant escape_javascript for ages.

@rafaelfranca rafaelfranca merged commit d65a15d into rails:master
@amatsuda amatsuda deleted the amatsuda:remove_j_alias_for_json_escape branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 9, 2011
  1. @amatsuda
This page is out of date. Refresh to see the latest.
7 activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -45,18 +45,11 @@ def html_escape(s)
# json_escape('{"name":"john","created_at":"2010-04-28T01:39:31Z","id":1}')
# # => {name:john,created_at:2010-04-28T01:39:31Z,id:1}
- # This method is also aliased as +j+, and available as a helper
- # in Rails templates:
- #
- # <%=j @person.to_json %>
- #
def json_escape(s)
result = s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
s.html_safe? ? result.html_safe : result
- alias j json_escape
- module_function :j
module_function :json_escape
Something went wrong with that request. Please try again.