Skip to content

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

Open

Description

@JuanitoFatas

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 ✏️ 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.

Activity

  1. hanmd82 commented on Jul 23, 2015

    @hanmd82
  2. cupnoodle commented on May 2, 2018

    @cupnoodle

    Was searching for what l method means, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions