Closed krsyoung closed 6 years ago
Would love to know the best way to approach this as well. @krsyoung did you have any luck?
I figured it out—add_breadcrumb accepts a Proc. In your example, I think you can just do this in your controller action:
add_breadcrumb 'projects', Proc.new{ company_projects_path(@company) }
Oh and @krsyoung re: your issue not wanting to repeat code, you can always use before_action
to add the breadcrumb before all actions:
class ProjectsController < ApplicationController
before_action do
@company = Company.find(params[:company_id])
add_breadcrumb @company.name + ' Projects', Proc.new{ company_projects_path(@company) }
end
def index
...
end
end
Brilliant @danderozier! Will poke around with this tonight and post back how I make out.
What's the scope of Proc? It seems the object isn't initialized while self is in that proc
Sorry guys, I never did get back to this (I ended up moving to a different solution). Thanks for keeping the issues clean @weppos !
Any suggestions on how to add breadcrumbs for nested resources? For example, my data model has Company with a has_many Projects where project is a nested resource of company. I'd like to see breadcrumbs like:
In the projects controller I want to do something like this:
However, the path needs to be
company_projects_path(@company)
, which I'm not sure how to go about considering the parameter is a symbol (:company_projects_path
). I'd rather not include theadd_breadcrumb
to each of my methods.Any suggestions?