skip to content

Tag & Link Helpers

Action View ships helpers for links, buttons, tags, numbers and text, and app/helpers adds your own to every view. Interviewers probe link_to vs button_to for non-GET calls and helpers vs presenters.

part ofRuby on Railsread it →
on this pageshow

explore

questions

6

In a Rails view, what do truncate(listing.notes, length: 40) and pluralize(listing.tracks_count, "track") return, and what do they escape?

level: juniorimportance: should knowfreq 30%

basics

~20 s

truncate cuts text to at most :length characters including the "..." omission (default 30), HTML-escapes the result unless escape: false, and marks it safe. pluralize returns the count plus the singular or plural word, such as "1 track" or "12 tracks".

open as a page

On a Rails listing page, what do number_to_currency(listing.price) and time_ago_in_words(listing.created_at) output, and which defaults trip people up?

level: middleimportance: should knowfreq 38%

basics

~20 s

number_to_currency formats a number with the current locale's currency settings (in en: "$1,234.50") but never converts it. time_ago_in_words returns an approximate distance from now such as "about 2 hours" or "3 days", with no "ago" and no past or future direction.

open as a page

In Action View, what does dom_id return for a saved record, a new record and a prefix argument, and why use it?

level: middleimportance: should knowfreq 45%

basics

~10 s

dom_id(listing) returns "listing_42"; for an unsaved record or the class it returns "new_listing"; dom_id(listing, :edit) returns "edit_listing_42". One naming rule lets views, Turbo Stream targets, forms and tests address the same element.

open as a page

In Action View, how does the tag builder (tag.div, tag.span) differ from content_tag, and what does it escape and expand?

level: middleimportance: should knowfreq 42%

basics

~20 s

tag.div(content, **attributes) is the current tag-builder API and content_tag(:div, content, attributes) the older positional form; both return an html_safe string, escape content and attribute values unless already safe, and expand data:, aria:, class arrays and boolean attributes.

open as a page

In a Rails app, why can a method from app/helpers/listings_helper.rb be called in every view, and what does include_all_helpers = false change?

level: seniorimportance: should knowfreq 32%

basics

~10 s

By default ApplicationController, as a direct ActionController::Base subclass, calls helper :all, so every app/helpers module reaches every view. With config.action_controller.include_all_helpers = false, each controller gets only its matching helper plus inherited ones.

open as a page