Look at the Doctors controller source code. How are the @doctors instance
variable used in the index view and the @doctor instance variable defined the
show view defined?
Ans: Look in the index method of the Doctor controller. You see the line
@doctors = Doctor.all
This line uses the ActiveRecord method all to get an array of Doctor objects from the
doctors table in the database.
To obtain the @doctor object in the Doctor show view, look at the bottom of the Doctor controller. The
set_doctor method uses the parameter id, which is
obtained from the URL of the page. If the id is 5, the URL is
http://localhost:3000/doctors/5
The id is then obtained from
params[:id]
which is then used to look up the corresponding doctor in the database:
@doctor = Doctor.find(params[:id])
If you look at the top of the Doctor controller, the call back
before_action calls the set_doctor method for the
methods show, update,
edit, and destroy.