Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Rails Routes and Resources A Route to a Read Action Finding a Model Object

yanchung cheng
yanchung cheng
11,640 Points

Finding A Model Object

Not sure what is up with this.

Instruction - We've set up the following in routes.rb: get '/pets/:id', to: 'pets#show' But that controller action method doesn't exist yet. Create it now. Within the method, use the :id parameter to find the matching Pet record, and assign the result to the @pet instance variable.

Error - Bummer! NameError: undefined local variable or method `pets' for PetsController:Class Did you mean? puts Restart Get Help Recheck work

My Answer - class PetsController < ApplicationController

@pet = Pet.find(pets[:id]) end

You were very close, you just need to add params. I had the same issue and later I was able to figure out. I am glad we were able to figure out and continue with our journey.

def show @pet = Pet.find(params[:id]) end

2 Answers

  def show 
    @pet = Pet.find(params[:id])

You're not defining a show method, I just had the same problem!

def show