Lesser Known Rails Helpers to Write Cleaner View Code

railsdesigner

Rails Designer

Posted on May 30, 2024

Lesser Known Rails Helpers to Write Cleaner View Code

This article was originally published on Rails Designer


Rails (or more precise ActiveSupport and ActionView) has some really great, quality-of-life helpers so that your code is cleaner. I want to highlight a few that I don't see used often, but will be great to add to your tool set.

These will highlight only helpers that are focused on code for the view-layer. If you purchased Rails Designer you might recognize most of these. 💡

This will be a swift article, covering all helpers in rapid succession.

class_names

The one helper I use. All. The. Time. It's actually an alias for token_list.

I wrote an entire article about this snippet—so do check that out—but in essence it works like this:

class_names("item", { active: item.for_sale?, "out-of-stock": item.out_of_stock? })
Enter fullscreen mode Exit fullscreen mode

current_path?

This method is used to determine if a given path matches the current page URL. Useful in views for setting active CSS classes, like so:

link_to "Home", root_path, class: ("active" if current_page?(root_path))
link_to "About", about_path, class: ("active" if current_page?(about_path))
Enter fullscreen mode Exit fullscreen mode

parameterize

parameterize is pretty well-known, but the fact it takes some arguments is lesser known:

"John Smith".parameterize(preserve_case: true) # => "John-Smith"
"John Smith".parameterize(separator: "_") # => "john_smith"
Enter fullscreen mode Exit fullscreen mode

upcase_first

"employee salary".upcase_first # => "Employee salary"
Enter fullscreen mode Exit fullscreen mode

downcase_first

"If I had read Alice in Wonderland".downcase_first # => "if I had read Alice in Wonderland"
Enter fullscreen mode Exit fullscreen mode

camelize

camelize can take a :lower argument (the default is :upper).

"visual_effect".camelize(:lower) # => "visualEffect"
Enter fullscreen mode Exit fullscreen mode

safe_join

safe_join is used to safely concatenate an array of strings, ensuring that each element is HTML escaped. You can use it instead of better known join.html_safe.

items = ["Home", "About", "Contact"]
navigation = items.map { |item| tag.li(link_to(item, "/#{item.downcase}")) } # => ["<li><a href=\"/home\">Home</a></li>", "<li><a href=\"/about\">About</a></li>", "<li><a href=\"/contact\">Contact</a></li>"]

safe_join(navigation)
Enter fullscreen mode Exit fullscreen mode

excerpt

This one is not something you can use often, but it's good to know about for those time you do. For example on search list page.

input = "Your team has been fantastic with their quick response times and thorough solutions. We've noticed an improvement in our operations due to your dedicated support."

excerpt(input, radius: 10)
# => ""...fantastic with their quick response times and...""

input = "The project has undergone several changes, and new deadlines have been set for the completion of each phase. The team needs to adjust accordingly to meet the revised goals."

excerpt(input, "deadline", radius: 20)
# => "...changes, and new deadlines have been set..."
Enter fullscreen mode Exit fullscreen mode

inquiry

I learned about this method in this PR. It converts strings or arrays into objects that allow you to query for presence in a more readable way

"admin".inquiry.admin? # => true
["pending", "active"].inquiry.pending? # => true
Enter fullscreen mode Exit fullscreen mode

to_sentence

Most Rails devs are aware of to_sentence. But it accepts a few arguments that are really useful.

['apple', 'banana', 'pear'].to_sentence
# => "apple, banana, and pear"

['apple', 'banana', 'pear'].to_sentence(last_word_connector: ' or ', two_words_connector: ' and ')
# => "apple, banana or pear"

['apple', 'banana'].to_sentence(two_words_connector: ' and also ')
# => "apple and also banana"

['apple', 'banana', 'pear'].to_sentence(words_connector: '; ', last_word_connector: '; and finally, ')
# => "apple; banana; and finally, pear"
Enter fullscreen mode Exit fullscreen mode

Do you know of any helper that is lesser known? Let me know!

💖 💪 🙅 🚩
railsdesigner
Rails Designer

Posted on May 30, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related