Handy crumbs variable for rails views.
I did this gem to:
- Have a dsl separate from the controllers to define crumbs.
- Make crumbs remember query parameters from previous requests.
Put this line in your Gemfile:
gem 'crumbs'
Then bundle:
$ bundle
NOTE: Because this gem stores queries in session, you may need a gem like redis-store to prevent cookies overflow.
Generate the configuration file:
$ bundle exec rails g crumbs:install
Use the crumb method to define a crumb:
Crumbs.define do
crumb 'pages#index', 'Home'
end
To translate the name you can use the t shortcut method, will prepend "crumbs" to the key:
Crumbs.define do
crumb 'pages#index', t('.home')
end
For dynamic names you can use a block, will receive the corresponding parameters:
Crumbs.define do
crumb 'products#show' do |params|
Product.find(params[:id]).name
end
end
If you need to add multiple crumbs to the same controller:
Crumbs.define do
namespace :admin do
controller :users do
crumb :index, 'Users'
crumb :edit, 'Edit user'
end
end
end
To disable crums in some controller:
class Api::BaseController < ApplicationController
skip_before_action :set_crumbs
end
Crumbs variable will be available in your views:
<% @crumbs.each do |crumb| %>
> <%= link_to crumb[:name], crumb[:url] %>
<% end %>
Any issue, pull request, comment of any kind is more than welcome!
I will mainly ensure compatibility to Rails, AWS, PostgreSQL, Redis, Elasticsearch and FreeBSD.
This gem is maintained and funded by museways.
It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.