rake db:create
to make your database.rails server
.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
name
attribute in the migration using t.string :name
. This is similar to how you defined your model schemas in Mongoose.pet.rb
model that requires the presence
of name
to be true
. Also require name
to be at least 3 characters. See the ActiveRecord validation docs for guidance.belongs_to :owner
.owner_id
to your Pets table. Use rails g migration AddOwnerIdToPet owner_id:integer
.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
called new
and a method called create
. In both methods, assign an instance variable @pet
to Pet.new
. Assign an instance variable @owner
to Owner.find(params[:owner_id])
.
Use an if / else
block (after your assignment of @owner
and @pet
) in PetsController#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
, add resources :pets
to the resources :owners do ... end
block. This will give you access to all seven RESTful routes for Pets.
views/pets
directory called new.html.erb
.form_for
to create a form for @pet
.<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!
owners_controller.rb
, create edit
and update
methods, the former renders a form to edit the owner, and later handles the PUT
request.rails g migration Add<SOMEATTRIBUTE>To<MODELNAME>
. Add validations for these new attributes in the models files (owner.rb
and pet.rb
). For example:
Pet
model by creating a new migration. Add a validation for breed
in your models/pet.rb
file. Edit your Pet creation form to include the new breed
attribute.Owner
model by creating a new migration. Add a validation for email
in your models/Owner.rb
file. Validate the email using a Regular Expression Edit your Owner creation form to include the new email
attribute.