link_toというヘルパ。リンクを作れるのでかなりよく使うヘルパ。
よくグローバルメニューとかで、現在表示しているページへのリンクは無効にするとかあるじゃん。
昔はこうやっていた。
<% if current_page?(root_path) %>
<%= link_to('トップページ', root_path) %>
<% end %>
<% if current_page?(sugoi_path) %>
<%= link_to('すごいページ', sugoi_path) %>
<% end %>
<% if current_page?(warosu_path) %>
<%= link_to('わろすなページ', warosu_path) %>
<% end %>
なんか、冗長にみえる。美しくない。
そして時は過ぎ、link_to_unlessというものを知ったんだ。
引数に条件を渡して、それが偽の時にだけリンクを貼るというもの。
これを使って書き直すとこうなる。
<%= link_to_unless(current_page?(root_path), 'トップページ', root_path) %>
<%= link_to_unless(current_page?(sugoi_path), 'すごいページ', sugoi_path) %>
<%= link_to_unless(current_page?(warosu_path), 'わろすなページ', warosu_path) %>
一行になった。これで満足していた自分がいた。
そして時は過ぎ、、、
link_to_unless_currentというものを知ったんだ!
<%= link_to_unless_current('トップページ', root_path) %>
<%= link_to_unless_current('すごいページ', sugoi_path) %>
<%= link_to_unless_current('わろすなページ', warosu_path) %>
これだ!!