Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Advanced Social Features in Ruby on Rails Create an Image Gallery Breadcrumbs

Rachel Bird
Rachel Bird
11,968 Points

Can't add breadcrumbs to my app

I tried to apply what I learned in this video, but it doesn't seem to be working.

Here's the error I'm getting:

NoMethodError in JobsController#show
undefined method `title' for nil:NilClass

Extracted source (around line #76):
74
75
76
77
78
79


   def add_breadcrumbs
     add_breadcrumb @category.title, @category
     add_breadcrumb @job.name, @job
   end
end

Rails.root: /home/vagrant/code/nonofficejobs

Application Trace | Framework Trace | Full Trace
app/controllers/jobs_controller.rb:76:in `add_breadcrumbs'
Request

Parameters:

{"category_id"=>"20",
 "id"=>"3"}

And here's the Jobs Controller:

jobs_controller.rb
class JobsController < ApplicationController
  before_filter :add_breadcrumbs

  def index
    @category = Category.find(params[:category_id])
    @jobs = @category.jobs.paginate(page: params[:page], per_page: 10)
    authorize @jobs
  end

  def new
    @category = Category.find(params[:category_id])
    @job = Job.new
    authorize @job
  end

  def show
    @category = Category.find(params[:category_id])
    @job = Job.find(params[:id])
    @posts = @job.posts.paginate(page: params[:page], per_page: 10)
    authorize @job
  end

  def edit
    @category = Category.find(params[:category_id])
    @job = Job.find(params[:id])
    authorize @job
  end

   def update
     @category = Category.find(params[:category_id])
     @job = Job.find(params[:id])
     authorize @job
     if @job.update_attributes(job_params)
       redirect_to [@category, @job]
     else
       flash[:error] = "Error saving job. Please try again"
       render :edit
     end
   end

  def create
     @category = Category.find(params[:category_id])
     @job = Job.new(job_params)
     @job.category = @category
     authorize @job
     if @job.save
       redirect_to [@category, @job], notice: "Job was saved successfully."
     else
       flash[:error] = "Error creating job. Please try again."
       render :new
     end
   end

   def destroy
     @category = Category.find(params[:category_id])
     @job = Job.find(params[:id])
     name = @job.name
     authorize @job

     if @job.destroy
       flash[:notice] = "\"#{name}\" was deleted successfully."
       redirect_to @category
     else
       flash[:error] = "There was an error deleting the job."
       render :show
     end
   end

   private

   def job_params
     params.require(:job).permit(:name, :description, :public)
   end

   def add_breadcrumbs
     add_breadcrumb @category.title, @category
     add_breadcrumb @job.name, @job
   end
end

1 Answer

Ben Wong
Ben Wong
2,652 Points

https://github.com/weppos/breadcrumbs_on_rails

I think they changed the naming so you have to put gem "breadcrumbs_on_rails" in your gemfile. Works for me after.