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 A Route to a Read Action Creating a Show View

bessiebarnes
bessiebarnes
88,917 Points

I'm stuck again on a challenge. I need help.

The show action method on the PetsController includes the following code:

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

Add a level 1 heading (<h1>) element to the template. Within that element, embed the Pet's name attribute. I tried <h1><%= pet.name %></h1>

app/views/pets/show.html.erb
<%  @Pet.find(params[:id]) %>
<h1> <%= pet.name %></h1>
end
Oswaldo Rangel
Oswaldo Rangel
3,554 Points

Hi Bessie, when you are working on a view that's referenced by a controller you already have access to the instance variable in this case @pet so you don't need to call it from the view, the controller is doing that work for you :), and in order to render the name just get the name from the instance variable it self like this:

@pet.name and remove <% @Pet.find(params[:id]) %>

end from the view.

cheers and happy coding!

1 Answer

bessiebarnes
bessiebarnes
88,917 Points

the answer is <h1> <%= @pet.name %></h1> Thanks Oswaldo!