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 Rails Routes and Resources Routes to Update Actions An Update Action

Aaron Feltman
Aaron Feltman
8,434 Points

Update the Pet object with the list of parameters returned from permit.

I am on step 4 of 5 on this challenge. below is the code I am using.
I have tried using just pet.update(pet_params) but it fails as well.

def update
        @pet = Pet.find(params[:id])
      params.require(:pet).permit(:name)  
      @pet = pet.update(pet_params)
  end
app/controllers/pets_controller.rb
class PetsController < ApplicationController

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

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

  def update
        @pet = Pet.find(params[:id])
      params.require(:pet).permit(:name)  
      @pet = pet.update(pet_params)
    end
end

3 Answers

Nickolas Fuentes
Nickolas Fuentes
14,016 Points

This line of code worked for me!

def update
    @pet = Pet.find(params[:id])
    params.require(:pet).permit(:name)
    @pet.update(params)
  end
Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Your current problems are being caused by the code pet.update(pet_params) on line 14. There is no such variable as pet in your code; pet and @pet are two different variables. Did you mean @pet?

Adriana Cabrera
Adriana Cabrera
14,618 Points

This is another way and what works for me too. I just want to share

def create # YOUR CODE HERE parametros = params.require(:pet).permit(:name) @pet = Pet.new(parametros) @pet.save redirect_to @pet

end