- Clone this repo.
- Run
rake db:create
to make your database. - Start your app with
rails server
. - Navigate to
localhost:3000
– you should see the Owner's index page.
Generate a Pet model using rails generate model Pet
. This will create a model file like this:
class Pet < ActiveRecord::Base
end
And a corresponding migration file, like this:
class CreatePets < ActiveRecord::Migration
def change
create_table :pets do |t|
# attributes go here
t.timestamps null: false
end
end
end
- Give your Pet table a
name
attribute in the migration usingt.string :name
. This is similar to how you defined your model schemas in Mongoose. - Add a validation to your
pet.rb
model that requires thepresence
ofname
to betrue
. Also requirename
to be at least 3 characters. See the ActiveRecord validation docs for guidance. - Associate your Pet model with an Owner. Use
belongs_to :owner
. - Generate a new migration to add a foreign key,
owner_id
to your Pets table. Userails g migration AddOwnerIdToPet owner_id:integer
. - Run
rake db:migrate
to get your database up to date.
- Generate a Pets controllers using
rails g controller Pets
. This will create a file like this:
class PetsController < ApplicationController
# routing actions go here
end
-
Define a method in
pets_controller.rb
callednew
and a method calledcreate
. In both methods, assign an instance variable@pet
toPet.new
. Assign an instance variable@owner
toOwner.find(params[:owner_id])
. -
Use an
if / else
block (after your assignment of@owner
and@pet
) inPetsController#create
to handle valid and invalid form submissions.
if @pet.save
@owner.pets << @pet
redirect_to @owner
else
render 'new'
end
- In
config/routes.rb
, addresources :pets
to theresources :owners do ... end
block. This will give you access to all seven RESTful routes for Pets.
- Create a file in your
views/pets
directory callednew.html.erb
. - Use
form_for
to create a form for@pet
. - Add an errors
<div>
so that an invalid form submission will cause the page to render with the errors displayed.
NOTE: If you need a refresher on syntax for form_for
and the errors, refer to the README for examples or look at views/owners/new.html.erb
.
If all is right, you should be able to create new pets and associate them with their owners. Additionally, your awesome error handling should display informative messages on how to properly submit your forms. Great job!
- In either
owners_controller.rb
, createedit
andupdate
methods, the former renders a form to edit the owner, and later handles thePUT
request. - Add new attributes to your models with new migrations using
rails g migration Add<SOMEATTRIBUTE>To<MODELNAME>
. Add validations for these new attributes in the models files (owner.rb
andpet.rb
). For example:- Add a breed attribute to the
Pet
model by creating a new migration. Add a validation forbreed
in yourmodels/pet.rb
file. Edit your Pet creation form to include the newbreed
attribute. - Add a email attribute to the
Owner
model by creating a new migration. Add a validation foremail
in yourmodels/Owner.rb
file. Validate the email using a Regular Expression Edit your Owner creation form to include the newemail
attribute.
- Add a breed attribute to the