weppos / breadcrumbs_on_rails

A simple Ruby on Rails plugin for creating and managing a breadcrumb navigation.
https://simonecarletti.com/code/breadcrumbs-on-rails
MIT License
942 stars 187 forks source link

How to pass parameters to paths for nested resources #111

Closed krsyoung closed 6 years ago

krsyoung commented 7 years ago

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:

# companies_path, @company, company_projects_path(@company), project_path(@project)
Companies / Bob the Builder / Projects / Tree fort

In the projects controller I want to do something like this:

class ProjectsController < ApplicationController
  add_breadcrumb 'projects', :company_projects_path

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 the add_breadcrumb to each of my methods.

Any suggestions?

danderozier commented 6 years ago

Would love to know the best way to approach this as well. @krsyoung did you have any luck?

danderozier commented 6 years ago

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) }
danderozier commented 6 years ago

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
krsyoung commented 6 years ago

Brilliant @danderozier! Will poke around with this tonight and post back how I make out.

AnwarShah commented 6 years ago

What's the scope of Proc? It seems the object isn't initialized while self is in that proc

krsyoung commented 6 years ago

Sorry guys, I never did get back to this (I ended up moving to a different solution). Thanks for keeping the issues clean @weppos !