To Examples

School3 Example

Directions

  1. Create a new Rails project named School3.
  2. Generate a Rails scaffold named Student with these fields.
    Field Datatype
    name string
    gender string
    phone string
  3. Generate a Rails scaffold named Course with these fields.
    Field Datatype
    course_num string
    title string
    credit_hours integer
  4. Generate a Rails model (not scaffold) named Enrollment with these fields.
    Field Datatype
    student_id integer
    course_id integer
  5. Make these additions to the model files:
    # In the School3/app/models/enrollment.rb
    belongs_to :course
    belongs_to :student
    
    # In School3/app/models/course.rb
    has_many :enrollments
    has_many :students, through: :enrollments
    
    # In School3/app/models/student.rb
    has_many :enrollments
    has_many :courses, through: :enrollments
    
  6. Source code for the School3 seed file School3/db/seeds.rb.
  7. At the bottom of the Course and Student index views, add a link to the index view of the other model.
  8. View the show pages with these URLs:
    http://localhost:3000/students
    http://localhost:3000/courses
    
  9. Modify the Student show view to show the courses in which the student is enrolled:
    <h3>Courses for <%= @student.name %></h3>
    
    <table>
      <tr>
        <th>Course Number</th>
        <th>Course Title</th>
        <th>Credit Hours</th>
      </tr>
    
      <% @student.courses.each do |c| %>
      <tr>
        <td><%= c.course_num %></td>
        <td><%= c.title %></td>
        <td><%= c.credit_hours %></td>
      </tr>
      <% end %>
    </table>
    
  10. Modify the Course show view to show the students that are enrolled in the course:
    <h3>Students in <%= @course.course_num %></h3>
    
    <table>
      <tr>
        <th>Name</th>
        <th>Gender</th>
        <th>Phone Number</th>
      </tr>
    
      <% @course.students.each do |s| %>
      <tr>
        <td><%= s.name %></td>
        <td><%= s.gender %></td>
        <td><%= s.phone %></td>
      </tr>
      <% end %>
    </table>
    
  11. Add a column to the Course index view to show the enrollment number in each course:
     <td><%= course.students.count %></td>
    

We now add the capability of enrolling students in new courses.

  1. Add these methods to the Student model: enrolled_in?, unenrolled_courses
    # Check if student is enrolled in a course
    def enrolled_in?(course)
      return self.courses.include?(course) 
    end
    
    # Get list of all courses that a 
    # student is not enrolled
    def unenrolled_courses
      return Course.all - self.courses
    end
  2. Add this method to the Student controller: add_course:
    def add_course
      @student = Student.find(params[:id])
      @course = Course.find(params[:course])
    
      if not @student.enrolled_in?(@course)
        enroll = Enrollment.new
        enroll.student_id = @student.id
        enroll.course_id = @course.id
        enroll.save
      end
      redirect_to @student
    end
    
    Add this form to the bottom of the Student show method before the Edit and Back links.
    <% if @student.enrollments.count < Course.count %>
      <p>Enroll in a course.</p>
      <%= form_tag add_course_student_path(@student) do %>
        <%= select_tag(:course,
            options_from_collection_for_select(
              @student.unenrolled_courses, :id, :course_num)) %>
        <%= submit_tag 'Enroll' %>
      <% end %>
    <% end %>
    
  • Add a route for the add_course method in School2/config/routes.rb. Replace the route resources :students to
     resources :students do
      member do
        post :add_course
      end
    end