LoginSignup
3
1

More than 3 years have passed since last update.

【Rails】初めてのパンくずリスト実装(gem gretel)

Last updated at Posted at 2020-04-19

パンくずリストを実装しました。gem gretelを使えば、設定ファイルとビューからの呼び出しのみで作成できます。
参) gretelのリファレンス

Alt text

手順

① gemの導入 → bundle install

Gemfile
gem 'gretel'

② 設定ファイルの作成

$ rails g gretel:install

③ 作成されたconfig/breadcrumb.rbにパンくずを定義
デフォルトのコメントは削除ok。

基本の構造
crumb :ビューで呼び出すためのパンくず名 do
  link "リンクの文字", リンク先のパス
  parent :1コ前のページのパス(ない場合は不要)
end

実際のコード

breadcrumb.rb
#ルート
crumb :root do
  link "フリマ", root_path
end

#マイページ
crumb :mypage do
  link "マイページ", mypage_index_path
end

# クレジットカード情報
crumb :mypage_cards_index do
  link '支払い方法', mypage_cards_index_path
  parent :mypage
end

# クレジットカード情報登録後の表示
crumb :mypage_cards_show do
  link 'クレジットカード情報', mypage_cards_show_path
  parent :mypage_cards_index
end
参考)itemsテーブルのnameカラムをリンク名にする場合
crumb :item do
  link Item.find(params[:id]).name, item_path
  parent :root
end

crumb :item do
  link @item.name, item_path
  parent :root
end

④ ビューで所望のパンくずを呼び出す

haml
.breadcrumbs
  - breadcrumb :パンくず名                    # 呼び出したいパンくずを指定。
  = breadcrumbs separator: " › "    # エスケープ文字で › を意味してる。

(備忘録)本番環境へのデプロイでエラーに陥った話

  • ローカル環境では、上記の記述で問題ないことを確認の上、デプロイすると、undefined method `breadcrumb'
    • 本番環境にインストール済みのgem表示($ bundle list)してみると、gretelが入ってなかったので、再度、bundle install。
    • 本番環境を一旦、kill → 再起動すると、エラーは消えた。単純に、反映できてなかっただけ。。
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1