Hi, I'm Nicholas Johnson!

software engineer / trainer / AI enthusiast

Models, Validation and Resources

We are going to create a Cat model to held information about a single cat. Use the model generator to create a simple model:

  rails g model cat name:string description:text

=code(code, :bash)

You will see we have made a nice cat class in the models directory, and also a migration in the db/migrations directory.

Run the migration using:

  rake db:migrate

This has updated your database schema.

Test using the console

Drop into the rails console by typing:

  rails c

Exercise - playing with cats

Now you can play with your Cat.

Try out the following commands in the console:

  c = Cat.create name: "Markey"
  c.name = "Dave"

What do they all do? You can do all this in your controller.

Exercise - A basket full of tiny little cats

Now create 10 more cats, and try out the following.

  Cat.where(name: "Dave")
  Cat.where("created_at > ?", Time.now - 1.minute)

Exercise - Making use of our cat

Now create a controller and route. The route should point to a method called show in the controller. It should be possible to visit:


In the controller retrieve the cat based on the id in the params hash.

Now make a view to display your cat. Output all the fields nicely.

Optional Extension, create an index page

Create a route of the form:


This should point to the index method in the cat_controller. This method should retrieve all of the cats with Cat.all.

Make a view which loops over the cats, displaying them all. To loop over a collection, you can do something like this:

  <% @cats.each do |cat| %>
    <%= cat.name %>
  <% end %>