jollygoodcode.github.io icon indicating copy to clipboard operation
jollygoodcode.github.io copied to clipboard

Ruby on Rails Helpers: h, j, l and t

Open JuanitoFatas opened this issue 9 years ago • 2 comments

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 &gt; 0 &amp; a &lt; 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

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.

JuanitoFatas avatar Jul 23 '15 03:07 JuanitoFatas

:+1: @JuanitoFatas

hanmd82 avatar Jul 23 '15 12:07 hanmd82

Was searching for what l method means, thanks!

cupnoodle avatar May 02 '18 22:05 cupnoodle