To Examples

Hospital3 Example

Directions

  1. Create a new Rails project named Hospital3.
  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 Hospital3/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. 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
    
  7. Add a new view to the doctors controller named directory:
    1. Create the new ERB file Hospital3/app/views/doctors/directory.html.erb.
    2. Add this directory method to the doctors controller file Hospital3/app/controllers/doctors_controller.rb:
       def directory
        @docs = Doctor.order(:name).all
        @women = Patient.where(gender: 'F').all
        @men = Patient.where(gender: 'M').all
      end
      
    3. Use this source code in the Hospital3/config/routes.rb file. Make sure that there is not an extra end statement after the comments. This routes file adds a route for the directory view. It uses collection instead of member because the collection view shows information from all of the rows in the database. member is used when referring to a specific row referenced by the row id.
  8. Display the directory view with this root URL:
    http://localhost:3000