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 Ruby on Rails 5 Basics Handling Requests Complete a Controller

steven smith
steven smith
2,050 Points

@pets = Pet.all render template: "pets/index.html.erb", layout:"application"

Unable to get it right as i am sure this code is correct or missing one or two, please correct me if you can

app/controllers/pets_controller.rb
class PetsController < ApplicationController

  def index
    # YOUR CODE HERE
  end

end
app/views/pets/index.html.erb
<p id="notice"><%= notice %></p>

<h1>Pets</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Birthdate</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @pets.each do |pet| %>
      <tr>
        <td><%= pet.name %></td>
        <td><%= pet.birthdate %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

2 Answers

Ethan Rivas
Ethan Rivas
9,979 Points

You just need to set an instance variable (@pets) equal to all the Pets (Pet.all) inside the index method if you want to pass the challenge:

@pets = Pet.all

Or the challenge is giving to you another error?

steven smith
steven smith
2,050 Points

Thank you for your answer,

Lesson rules: never learning anything in a very late night.

i was typing '@pets = Pet.all render template: "pets/index.html.erb", layout:"application" ' and puzzle why i get failed. it was all @pets =Pet.all, without render template.

Ethan Rivas
Ethan Rivas
9,979 Points

Haha yes, take a break and try to "think" in your head what have you learned, and if after that you feel like you can keep doing it, do it :)