Created
March 18, 2021 21:43
-
-
Save dpaluy/014166aa521f78cde2c81b60d51e841c to your computer and use it in GitHub Desktop.
Breadcrumbs in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ApplicationController < ActionController::Base | |
| ... | |
| helper_method :breadcrumbs | |
| def breadcrumbs | |
| @breadcrumbs ||= [] | |
| end | |
| def add_breadcrumb(name, path = nil) | |
| breadcrumbs << Breadcrumb.new(name, path) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PostsController < ApplicationController | |
| before_action :set_breadcrumbs | |
| def index | |
| @posts = Post.all | |
| end | |
| def show | |
| @post = Post.find(params[:id]) | |
| add_breadcrumb(@post.title, @post) | |
| end | |
| def new | |
| @post = Post.new | |
| add_breadcrumb("New Post") | |
| end | |
| private | |
| def set_breadcrumbs | |
| add_breadcrumb("Admin", admin_home_path) if Current.user.admin? | |
| add_breadcrumb("Posts", posts_path) | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: https://boringrails.com/tips/boring-breadcrumbs-rails