In a Rails view, what do truncate(listing.notes, length: 40) and pluralize(listing.tracks_count, "track") return, and what do they escape?
answer
- length counts the omission
- default length 30, omission ...
- separator: cuts on a word boundary
- escaped unless escape: false
- count prefixed to the word
basics
~20 struncate 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".
solid answer
~40 s`truncate(text, length: 40)` returns the text unchanged when it fits; otherwise it keeps `40 - 3` characters and appends `"..."`, so the omission counts toward the length. `separator: " "` backs up to the last space so words are not split, `omission:` changes the marker, and a block appends extra content such as a "more" link only when the text was cut. The result is HTML-escaped and marked `html_safe`; `escape: false` skips the escaping and still marks it safe, so it must never be used on user input, and cutting HTML can leave unclosed tags. A `nil` text returns `nil`. `pluralize(count, "track")` returns `"1 track"`, `"0 tracks"` or `"12 tracks"`, using the inflector for the current locale, or `plural:` when given. It returns a plain string, which ERB escapes on output like any other.
code
erb · 9 lines<%# listing.notes = "Kind of Blue, first pressing, near mint" %>
<%= truncate(listing.notes, length: 20) %>
<%# => Kind of Blue, fir... %>
<%= truncate(listing.notes, length: 20, separator: " ") %>
<%# => Kind of Blue,... %>
<%= truncate(listing.notes, length: 20) { link_to "more", listing_path(listing) } %>
<%# => Kind of Blue, fir...<a href="/listings/42">more</a> %>
<%= pluralize(listing.tracks_count, "track") %>
<%# 1 => 1 track, 0 => 0 tracks, 12 => 12 tracks %>go deeper
Recall the default length and omission, that the omission counts, and that pluralize prefixes the count.
Explain the escape-then-mark-safe order, separator:, the block that appears only on cut text, and nil handling for both helpers.
Flag escape: false on user content and truncation of HTML in review, and move pluralized sentences into translations when the app is localized.
Decide which text shortening belongs in the server and which in CSS, and keep user-content rendering rules consistent across helpers.
## Two small text helpers A vinyl listing card shows the seller's notes and the number of tracks. Action View's **TextHelper** covers both: ```erb <p><%= truncate(listing.notes, length: 40, separator: " ") %></p> <p><%= pluralize(listing.tracks_count, "track") %></p> ``` ## truncate: how the length is counted `truncate(text, options = {}, &block)` relies on Active Support's `String#truncate` and then handles escaping. The options: | Option | Default | Meaning | |---|---|---| | `length` | `30` | maximum characters **including** the omission | | `omission` | `"..."` | marker appended when text is cut | | `separator` | none | string or regexp to break at, such as a space | | `escape` | `true` | HTML-escape the result | The arithmetic trips people up. With the default omission, `truncate(text, length: 20)` keeps **17** characters and adds `"..."`: - `truncate("Kind of Blue, first pressing, near mint", length: 20)` returns `"Kind of Blue, fir..."`. - Adding `separator: " "` backs up to the last space within those 17 characters: `"Kind of Blue,..."`. - A text of 20 characters or fewer comes back whole, with no omission. A block is appended **only when the text was cut**, which suits a "read more" link: `truncate(listing.notes, length: 80) { link_to "more", listing_path(listing) }`. The block's output may push the total past `length`. ## truncate and escaping 1. The text is truncated as plain characters first. 2. The result is HTML-escaped (unless `escape: false`). 3. The result is marked `html_safe`, so ERB does not escape it a second time. Two consequences follow: - With the default, notes containing `<b>` show the tag as text, and the length counts the raw `&` or `<` as one character before escaping. - `escape: false` marks the raw text safe **without sanitizing it**. On user-written notes that is an XSS hole, and on trusted HTML it can still cut through a tag and leave invalid markup. If the source is HTML, strip or sanitize it first and then truncate. `truncate(nil)` returns `nil`, so an empty notes field renders nothing. ## pluralize: count plus word `pluralize(count, singular, plural_arg = nil, plural: plural_arg, locale: I18n.locale)` returns `"#{count} #{word}"`: - `count` of `1` (or `"1"`, `"1.0"`) keeps the singular: `"1 track"`. - Anything else uses `plural:` if given, otherwise the inflector for the locale: `"0 tracks"`, `"12 tracks"`, `pluralize(2, "person")` gives `"2 people"`. - A `nil` count is printed as `0` with the plural word. It returns an ordinary string, not a safe one, so ERB escapes it like any other value. It differs from Active Support's string inflection, which returns only the word: the helper always prefixes the count. ## When to reach past them - Text that must be cut by words for many languages, or by rendered width, is a CSS or front-end concern (line clamping), not a `truncate` job. - Plural rules beyond English, and full sentences such as "No tracks yet", belong in translations with `count:` rather than `pluralize`. ## Mistakes interviewers listen for | Belief | What Action View actually does | |---|---| | `length: 20` keeps 20 characters plus "..." | the omission is inside the 20 | | truncate splits on words | only with `separator:` | | `escape: false` sanitizes | it marks raw text safe with no cleaning | | the block is always appended | only when the text was actually cut | | `pluralize` returns just the word | it always prefixes the count | Both helpers are small, but the escaping detail is the part reviewers care about: any helper that marks text safe is a place where user input can turn into markup, so the default `escape: true` should stay unless the text is known to be clean HTML.
- Why is truncate(listing.notes, escape: false) dangerous when sellers write the notes?With `escape: false` the truncated text is marked `html_safe` without any sanitizing, so a seller's `<script>` or event-handler attribute reaches the page as markup. Keep the default escaping, or sanitize the HTML first and accept that cutting it can still break tags.
- When would you pass plural: to pluralize?When the inflector's guess is wrong for your word or you want a different noun for many, such as `pluralize(count, "LP", plural: "LPs")`. `plural:` is used for every count other than 1.
- truncate's length excludes the three-character omission
- truncate breaks on word boundaries by default
- truncate with escape: false sanitizes the HTML before marking it safe
- pluralize returns only the word, without the count