To Lecture Notes

IT 231 -- 9/23/15

Review Questions

  1. In a Rails scaffold project, why should you replace the coffee-script-source gem with version 1.8.0 rather than deleting the javascript_include_tag and csrf_meta_tags method calls on the layout page?
    Ans: Because JavaScript is used in a scaffold project, for example, when deleting a row on the index view.
  2. What does a one-to-many relationship do for you?
    Ans: Consider the doctors (primary table) and patients (secondary table).
    1. For each @patient object, you get a Doctor method that returns the Doctor object to which the patient belongs. Then you can get any field from that doctor that you need, for example @patient.doctor.name.
    2. For each @doctor object, you get a patients array that contains all the patients that belong to the doctor. You can then loop over all these patients to display information about them, for example:
      <% @doctor.patients.each do |p| %>
        <p>Name: <%= p.first_name %> <%= p.last_name %><br />
           Phone Number: <%= p.phone_number %></p>
      <% end %>
      
  3. 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.

  4. Finish the Hospital1 Example. For the Patient new and edit views:
    1. replace the textfield for entering the gender with a dropdown menu showing F and M,
    2. replace the textfield for entering the doctor_id with a a dropdown menu that shows the doctors name but sends the corresponding doctor id to the database table.
  5. On the Doctor index view, add a link to the Patient index view; on the Patient index view, add a link to the Doctor index view.

CSS file for Course Website

Validations

The File Field Control

Project 2