導入
パンくずリストの導入にはgretelというgemが便利。
gem 'gretel'
% bundle install
% rails g gretel:install
上記のコマンドで、configディレクトリ直下にbreadcrumbs.rbが作成される。
パンくずリストの作成
例として、トップページ→投稿一覧→投稿詳細という階層のリストを作成する。
crumb :root do
link "Top", root_path
end
crumb :post_index do
link "投稿一覧", posts_path
parent :root
end
crumb :post_show |post| do
link "投稿詳細", post_path(post)
parent :post_index
end
対象のviewファイルに下記のように記載する。
<% breadcrumb :post_index %>
<% breadcrumb :post_show, @post %>