jollygoodcode.github.io
jollygoodcode.github.io copied to clipboard
Ruby on Rails Helpers: h, j, l and t
There are four one-char Rails helpers that seasoned developers use all the time for their simplicity and expressiveness:
h
, alias for html_escape
h
is a helper for escaping HTML tag characters.
Reference: http://api.rubyonrails.org/classes/ERB/Util.html#method-c-html_escape
> h("is a > 0 & a < 10?")
=> is a > 0 & a < 10?
j
, alias for escape_javascript
j
is a helper for escaping carriage returns, single and double quotes in JavaScript to prevent malicious JavaScript from being executed via user's input.
Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-escape_javascript
$('#comment-<%= @comment.id %>').html('<%= j render 'form', comment: @comment %>');
l
, alias for localize
l
is a helper for localizing Date
and Time
objects to local formats.
Reference: http://api.rubyonrails.org/classes/AbstractController/Translation.html#method-i-localize
<%= l(Time.current) %>
# => "Wed, 22 Jul 2015 16:35:27 +0800"
t
, alias for translate
t
is a helper for looking up text translations.
Reference: http://api.rubyonrails.org/classes/AbstractController/Translation.html#method-i-translate
<%= t("site.title") %>
# => "My Awesome Store"
Thanks for reading!
@JuanitoFatas :pencil2: Jolly Good Code
About Jolly Good Code
We specialise in Agile practices and Ruby, and we love contributing to open source. Speak to us about your next big idea, or check out our projects.
:+1: @JuanitoFatas
Was searching for what l
method means, thanks!