To Examples

Hospital1 Example

Directions

  1. Create a new Rails project named Hospital1.
  2. Generate a Rails scaffold named Doctor with these fields.
    Field Datatype
    name string
    office_num integer
    phone_num string
  3. Generate a Rails scaffold named Patient with these fields.
    Field Datatype
    last_name string
    first_name string
    gender string
    phone_num string
    doctor_id integer
  4. Source code for the seed file Hospital1/db/seed.rb.
  5. View the index pages of the Doctor and Patient index pages with these URLs:
    http://localhost:3000/doctors
    http://localhost:3000/patients
    
  6. Make these modifications to the Hospital1 Project:
    1. Add these validations to the Doctor and Patient models:
      1. Require name for Doctor and the first and last names for Patient to be non-empty:
        To the Doctor model, add this validation:
        validates :name, presence: true
        
        To the Patient model, add these validations:
        validates :first_name, presence: true
        validates :last_name, presence: true
        
      2. Require the office number for Doctor to be between 1 and 799.
        validates :office_num, numericality: {
           greater_than_or_equal_to: 1,
           less_than_or_equal_to: 799  }
        
      3. Require the phone number for Doctor to be in the form ???/???-????:
        To the Doctor model, add this validation:
        validates :phone_num, format: { with: { /\d{3}\/\d{3}-\d{4}/ },
          message "phone number should be formatted as ???/???-????" } 
        
      4. Require the gender for Patient to be either "F" or "M".
        Add this validation to the Patient model:
        validates :gender, inclusion: { in: %w( F M ) }
        
    2. Set up a one-to-many relationship between the Doctor and Patient models.
      Add this line to the Doctor model:
      has_many :patients
      
      Add this line to the Patient model:
      belongs_to :doctor
      
    3. On the index and show views for the Patient model, show the doctor's name instead of the doctor's id.
      In the index view of the Patients controller, replace:
      patient.doctor_id
      
      with
      patient.doctor.name
    4. On the show view of the Doctors controller, show all of that doctor's patients.
      Add this code to the show view of the Doctors controller:
      <table>
        <tr>
          <tr>Name</tr>
          <tr>Gender</tr>
          <tr>Phone Number</tr>
        </tr>
      
        <% @doctor.patients.each do |p| %>
        <tr>
          <td><%= p.first_name %> <%= p.last_name %></tr>
          <td><%= p.gender %></td>
          <td><%= p.phone_num %></td>
        </tr>
        <% end %>
      </table>  
      
    5. On the new and edit views, provide a dropdown menu showing "F" and "M" to enter the gender.
    6. Rather than hardcoding the doctor id, provide a dropdown menu on the Patient new and edit views that shows the doctor names but enters the doctor id in the database table.