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 Create Actions Controller Action to Create Pets

I cannot guidance for this question, take the pet parameter group returned from require, and use the permit method on it

take the pet parameter group returned from require, and use the permit method on it to allow the name parameter. Can someone give guidance. Thanks

app/controllers/pets_controller.rb
class PetsController < ApplicationController

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

  def new
    @pet = Pet.new
  end

  def create
    pet_params = permit(:name)
    @pet = Pet.new(pet_params)
    @pet.save
  end

end

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

You're close! To clear Task 1, I'm guessing you wrote code like this:

  def create
    pet_params = params.require(:pet)
  end

That stored the set of parameters you need in the pet_params variable. Now, you need to take the value in pet_params, and call permit on that:

  def create
    pet_params = params.require(:pet)
    pet_params = pet_params.permit(:name)
  end

Or, you can save yourself some typing and just call permit directly on the value returned from require, and assign that to the pet_params variable:

  def create
    pet_params = params.require(:pet).permit(:name)
  end