クイックリファレンス

Railsビューヘルパー チートシート

Rails組み込みビューヘルパーのクイックリファレンス:数値、通貨、相対日付、文字列フォーマット、サニタイズ、アセットタグ。

· 公開日

Action Viewヘルパーは、ビューテンプレート内でテキスト、数値、日付、メディアアセットを簡潔かつ綺麗にフォーマットするためのメソッドを提供します。

数値・通貨ヘルパー

<%= 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" -->

日付・時刻ヘルパー

<!-- 相対的な経過時間表現 -->
<%= distance_of_time_in_words(Time.now, 3.hours.from_now) %>
<!-- => "about 3 hours" -->

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

<!-- セマンティックなHTML5 <time> タグ -->
<%= time_tag(article.published_at) %>
<!-- => '<time datetime="2026-10-25T00:00:00Z">October 25, 2026</time>' -->

テキスト・文字列ヘルパー

<%= 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>" -->

アセット・タグヘルパー

<%= 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" %>

参考資料

関連テーマ