- Generate a model for
Appointment
s. It should have:
- a datetime field for the
time
of the appointment, - a string field for the
reason
for the appointment, - a string field for the name of the
veterinarian
, - and a foreign key
pet_id
for the pet whom the appointment belongs to.
-
Check the migration that was generated, and
rails db:migrate
when you feel comfortable with it. -
Check that the
Appointment
model inapp/models/appointment.rb
has code indicating each appointment belongs to a pet. -
Update the
Pet
model inapp/models/pet.rb
so it can have many appointments. -
In the Terminal, open up the Rails console. Choose an existing pet, and create at least one appointment for it.
irb(main):001:0> pikachu = Pet.find_by name: "Pikachu" #might have to recreate
irb(main):001:1> checkup = Appointment.create({
irb(main):001:2> time: DateTime.now + 1, # one day from now
irb(main):001:3> reason: "annual checkup",
irb(main):001:4> veterinarian: "Nurse Joy",
irb(main):001:5> pet: pikachu
irb(main):001:6> })
Check that your appointment and pet have the values you expect (especially the appointment's pet_id
).
- Add seed data for 6 appointments to your
seeds.rb
. Use thePet
seed section as an example of how to do this.
- You can use the
random_date_after_now
method if you'd like, for the appointment date. - Also, add a line at the beginning of the file that destroys all existing appointments.
We could nest routes further, but usually we want to keep nesting to no more than 1 level deep. To accommodate this, create a few appointment routes nested inside pets.
# config/routes.rb
resources :owners do
resources :pets, only: [:index, :new, :create]
end
resources :pets, only: [:show, :edit, :update, :destroy] do
resources :appointments, only: [:new, :create]
end
Run rails routes
in the Terminal to see the new routes. Read or review the Rails Guide on Routing's Nested Resources section, especially "2.7.2 Shallow Nesting," for the source of this plan and a shorter alternative syntax.
- On the pet show view, add a list of all the pet's appointments, showing time, veterinarian name, and reason. We'll do this instead of having the appointments on a separate appointment index view.
Hint: The view has access to `@pet` right now. How would you find that pet's appointments?
If you have your model relationship set up correctly, you should be able to get the appointments with `@pet.appointments`.-
Add a
link_to
the new appointments form on the pet show view. -
Generate an appointments controller, and add a
new
action. Have the controller retrieve the data necessary to create a new appointment for a particular pet. Remember, you'll want to use a dummyAppointment.new
here. -
Create a new appointment view that renders a new appointment form. The form should have fields for at least the
veterinarian
name and the appointmentreason
. You'll need to make use ofform_for
with nested resources again. Hint: use your new pet form for inspiration.
Hint: what kind of field to use for a date and time?
`datetime_local_field`!- Add a
create
action to the appointments controller that creates an appointment for the correct pet based on whitelisted params.