- If you haven't already, fork this repo, and clone it into your WDI class folder on your local machine.
- Run
bundle
in the Terminal to install gems from the Gemfile. - Run
rake db:create db:migrate
in the Terminal to create your local database and run the migrations. - Run
rails s
in the Terminal to start your server. - Navigate to
localhost:3000
in the browser - you should see the Rails welcome page.
-
In addition to owners, this app has a model for pets. In this app, each owner will have many pets, and each pet will belong to one owner. Change the
Owner
andPet
models to reflect this relationship. -
Add a foreign key to the
Pet
table so that each pet stores a reference to its owner.
See the ActiveRecord Migrations Rails guide for how add a foreign key to a table that's already been created.
Hint: how to generate migration to add foreign key?
The example from the docs adds a user foreign key to a products table, by running `bin/rails generate migration AddUserRefToProducts user:references`, but you can also use something like `rails g migration AddUserRefToProducts user:belongs_to`. Just replace the example models with the names this app needs!- In the Terminal, open up the Rails console, and create a few associated instances of pets and owners.
irb(main):001:0> ash = Owner.create({
irb(main):001:1> first_name: "Ash",
irb(main):001:2> last_name: "Ketchum",
irb(main):001:3> email: "[email protected]",
irb(main):001:4> phone: "(03) 1234-5678"
irb(main):001:5> })
irb(main):001:0> pikachu = Pet.create({
irb(main):001:1> name: "Pikachu",
irb(main):001:2> breed: "pokemon"
irb(main):001:3> })
irb(main):001:0> ash.pets << pikachu
Compare ash
's id
with pikachu
's new owner_id
.
-
Add validations to the
Pet
model. Pets are required to have bothname
andbreed
, andname
cannot be longer than 255 characters. Test your validations withrspec spec/models/pet_spec.rb
. -
At this point, you can "comment in" the parts of the
db/seeds.rb
file that relate to pets and runrake db:seed
. This will destroy all old pets and create new ones.
Nest routes for pets inside the routes for owners. Start with just an index route:
# config/routes.rb
resources :owners do
resources :pets, only: :index, :show, :new, :create
end
Run rake routes
in the Terminal to see the new routes. You can see a table of nested routes and each route's purpose in the Routing Rails guide.
-
On the owner show view, add a "view this owner's pets" link. The link should go to the path that will show all of that owner's pets. Reference the route table in the Routing Rails guide.
-
The pets index view will show all the pets for a particular owner. Finish filling in the pets index view template. The pets index view should list the names of all pets belonging to the owner.
-
Fill in the
pets#index
controller action so that the view has the necessary information to display. -
Add a link to each pet's name that leads to the pet's show page.
-
Fill in the pet show template to display the pet's name and breed, and fill in the
pets#show
controller action to enable this view.
-
On the pet index view, add a link to the path for the new pet form. Remember to reference the route table in the Routing Rails guide.
-
Create a new pet view that renders a form. The form should have text fields for the pet's
name
andbreed
. Research the syntax forform_for
with nested resources.
Hint: research help?
The top google result for "form_for nested resource" is a StackOverflow question, and the top answer has the necessary syntax. Take a look at [the answer](http://stackoverflow.com/a/4611932).- Add a
new
action to the pets controller, and have the controller retrieve the data necessary to create a new pet for a particular owner.
Hint: what data is necessary?
Like with most `new` actions, you'll want a dummy new pet to use with the `form_for` helper. Since this pet is being added to a particular owner, you'll also need to use that owner's information.-
Add a
create
action to the pets controller. This action will need to find the proper owner, make and save a new pet, and add the pet to the owner's list of pets. It can redirect to the new pet's show page when the creation is successful. -
If there is an error in the creation of the new pet, add a flash message. Redirect back to the new pet form for that owner.
Three types of stretch challenges lay before you. Pick and choose any you find interesting. Solutions are provided.
Practice using path helper methods by adding link_to
s to help users move among the pages of your site. Consider adding:
- a link to the owner index from the owner show page
- a link to the owner's show page from the list of their pets
- a link to the owner's list of pets from the pet show page
- a link to the site index on every page (keep it DRY!)
Create or fill in routes, controllers, and views for the missing crud actions for owners and pets. Choose one route at a time. It might be easiest to start with destroy
; just remember the difference between destroy
and delete
!
Practice your Ruby skills - get more time with the Date
and DateTime
built-in classes.
-
Generate and run a migration to add a
date_of_birth
field to thePet
model. The type of this field should bedate
. Display the pet'sdate_of_birth
in the view forpets#show
. -
Fill in the
Pet
model'sdate_of_birth_cannot_be_in_the_future
method. This method should add an error to the validation errors if the pet'sdate_of_birth
is in the future. See the Validations Rails guide. -
Fill in the
Pet
model'sage
instance method. If the pet instance has adate_of_birth
, this method should calculate and return the pet's age in years (as a decimal). If the pet doesn't have adate_of_birth
, theage
method should returnnil
. Display the pet'sage
in the view forpets#show
.