Riferimento rapido

Guida rapida ai view helper di Rails

Guida rapida ai view helper integrati di Rails: numeri, valute, date relative, stringhe, sanitizzazione e tag delle risorse.

· Pubblicato

Gli helper di Action View forniscono metodi di formattazione chiari per testo, numeri, date e risorse multimediali all'interno dei template.

Helper per Numeri e Valute

<%= number_to_currency(1234.56) %>
<!-- => "$1,234.56" -->

<%= number_to_currency(1234.56, unit: "€", format: "%u %n") %>
<!-- => "€ 1,234.56" -->

<%= number_to_percentage(85.4, precision: 1) %>
<!-- => "85.4%" -->

<%= number_to_human_size(15485863) %>
<!-- => "14.8 MB" -->

<%= number_with_delimiter(12345678) %>
<!-- => "12,345,678" -->

Helper per Date e Orari

<!-- Tempo relativo leggibile -->
<%= distance_of_time_in_words(Time.now, 3.hours.from_now) %>
<!-- => "about 3 hours" -->

<%= time_ago_in_words(user.created_at) %>
<!-- => "3 days ago" -->

<!-- Tag semantico HTML5 <time> -->
<%= time_tag(article.published_at) %>
<!-- => '<time datetime="2026-10-25T00:00:00Z">October 25, 2026</time>' -->

Helper per Testo e Stringhe

<%= truncate("The quick brown fox jumps over the lazy dog", length: 20) %>
<!-- => "The quick brown f..." -->

<%= pluralize(3, "comment") %>
<!-- => "3 comments" -->

<%= pluralize(1, "comment") %>
<!-- => "1 comment" -->

<%= highlight("Text with keywords highlighted", "keywords") %>
<!-- => 'Text with <mark>keywords</mark> highlighted' -->

<%= simple_format("Paragraph 1

Paragraph 2") %>
<!-- => "<p>Paragraph 1</p><p>Paragraph 2</p>" -->

Helper per Risorse e Tag

<%= link_to "Profile", user_path(@user), class: "nav-link" %>
<%= button_to "Delete", user_path(@user), method: :delete, data: { turbo_confirm: "Are you sure?" } %>
<%= image_tag "avatar.png", alt: "User Avatar", size: "64x64" %>

Riferimenti

Argomenti correlati