Ruby on Rails Tutorial: Creating an Educational Platform

Ruby on Rails Blog Post Tutorial: Creating an Educational Platform

This tutorial series will guide you through building a Ruby on Rails application tailored for an educational platform. We’ll use MySQL as the database and Tailwind CSS for styling. The app will feature two user roles—tutors and customers—with functionalities like account creation, posting lesson categories, booking lessons, and managing availability.

Part 1: Setting Up the Rails Application with Tailwind CSS and MySQL

Specifications:

  • Ruby on Rails application using Tailwind CSS for styling.
  • MySQL database with a root user and no password.

Steps:

  1. Create New Rails Application
    • Filename and location: Run this command in your terminal where you
    • want your project folder.
    • Command:
      • rails new educational_platform -d mysql --css tailwind
        
  2. Configure Database
    • Filename and location: config/database.yml
    • Code snippet:
  3. Install Tailwind CSS
  4. Run Database Creation
    • Command to run:

This section sets the foundation for our application, ensuring we have the necessary tools and configurations to proceed with building the platform.

Next Steps:

In the following section, we will focus on creating the tutor and customer tables with user functionality, using Devise for user authentication and management. We will also set up the basic roles for these users.

Continuation Plan:

The next part will cover setting up Devise with our user roles and adding the necessary database migrations for tutors and customers.

Part 2: Setting Up User Models with Devise

In this section, we’ll integrate Devise for user management, define our two main roles—tutor and customer—and set up the corresponding database tables.

Specifications:

  • Use Devise for user management.
  • Create tutor and customer roles with user functionality.

Steps:

  1. Add Devise to Gemfile
    • Filename and location: Gemfile
    • Add this line:
  2. Bundle Install and Initialize Devise
    • Run the following commands in your terminal:
  3. Configure Devise
    • Instructions and configuration settings will be printed in your terminal. Follow these to configure action mailer in config/environments/development.rb:
  4. Generate User Model with Devise
    • Command:
  5. Add Role to Users
    • Modify the migration file created by Devise (look for a file under db/migrate/ with a name that starts with a timestamp and includes devise_create_users.rb):
  6. Create Tutor and Customer Tables
    • Filename and location: db/migrate/YYYYMMDDHHMMSS_create_tutors.rb and db/migrate/YYYYMMDDHHMMSS_create_customers.rb
    • Commands to generate migrations:
  7. Run Migrations
    • Command:
  8. Setting Up Roles with Enum
    • Filename and location: app/models/user.rb
    • Code snippet to define roles within the User model:

This setup allows for a flexible user system where a user can either be a tutor or a customer, leveraging Devise for authentication and management.

Part 3: Customizing Devise for Tutor and Customer Accounts

In this part of the tutorial, we’ll customize Devise to handle our dual-role system, creating distinct pathways for tutor and customer registration and login.

Specifications:

  • Customized login and registration for tutors and customers.

Steps:

  1. Generate Devise Controllers
    • To customize the behavior of Devise, we’ll need to generate its controllers.
    • Command:
      • rails generate devise:controllers users
        
  2. Configure Routes for Custom Controllers
    • Filename and location: config/routes.rb
    • Modify the routes to use the new controllers:
    • Code snippet:
      • devise_for :users, controllers: {
           sessions: 'users/sessions',
           registrations: 'users/registrations'
        }
        
  3. Separate Registration Paths
    • We’ll create separate registration forms for tutors and customers. First, override the new action in users/registrations_controller.rb to check the role parameter and render different views:
    • Code snippet:
      • class Users::RegistrationsController < Devise::RegistrationsController
           def new
              super do |resource|
                 resource.role = params[:role] if params[:role].present?
              end
           end
        end
        
    • Create separate views for each role in app/views/devise/registrations/ named new_tutor.html.erb and new_customer.html.erb.
  4. Conditional Routing After Sign Up
    • Override the after_sign_up_path_for method in users/registrations_controller.rb to redirect users based on their role:
    • Code snippet:
      • def after_sign_up_path_for(resource)
           resource.tutor? ? tutor_dashboard_path : customer_dashboard_path
        end
        
  5. Create Tutor and Customer Dashboards
    • Commands to generate controller and views:
    • rails generate controller Tutors dashboard
      rails generate controller Customers dashboard
      
    • Setup basic routes in config/routes.rb:
    • get 'tutor_dashboard', to: 'tutors#dashboard'
      get 'customer_dashboard', to: 'customers#dashboard'
      

This configuration allows us to manage tutor and customer accounts separately, giving each a customized registration form and dashboard.

Next Steps:

Next, we will set up the models and controllers for lessons and categories, enabling tutors to post lesson details under specific categories which can be parent or child categories. We’ll also work on the category management system.

Continuation Plan:

The upcoming section will focus on creating the lessons and categories functionality, including database models, associations, and nested category support.

Part 4: Setting Up Lessons and Categories

In this section, we will set up the models and controllers for managing lessons and categories, enabling tutors to post lesson details under specific categories that can be either parent or child categories. This will help organize the lessons into a structured format.

Specifications:

  • Create a hierarchical category system for lessons.
  • Enable tutors to post lessons under these categories.

Steps:

  • Establish Relationships in Models
    • Modify Lesson and Tutor models to include associations:
    • Code snippet for Lesson model:
      • class Lesson < ApplicationRecord
          belongs_to :tutor
          belongs_to :category
        end
        
    • Code snippet for Tutor model update:
      • class Tutor < ApplicationRecord
          has_many :lessons
          belongs_to :user
        end
        
  • Run Migrations
    • After setting up the models with their associations, execute the migrations to update the database:
    • Command:
      • rails db:migrate
        
  • Create Controllers for Categories and Lessons
    • Create controllers to manage categories and lessons, allowing CRUD operations:
    • Commands:
      • rails generate controller Categories index new create edit update destroy
        rails generate controller Lessons index new create edit update destroy
        
  • Implement Views for Category Management
    • Create views for each action in the Categories controller to allow users to create, edit, and view categories:
    • Ensure the views use forms that are appropriate for nested categories, possibly using JavaScript to enhance user interaction.
  • Setup Routes
    • Define the necessary routes in config/routes.rb for handling category and lesson management:
    • Code snippet:
      • resources :categories do
          resources :lessons
        end
        
  • This section has set up a robust system for managing lessons and categories, allowing tutors to categorize their lessons effectively and customers to browse lessons according to these categories.

    Next Steps:

    Next, we will develop the functionality for tutors to post lessons within their chosen categories and for customers to view available lessons. We’ll also add post functionality and category selection for tutors.

    Continuation Plan:

    The next part will cover integrating the post functionality for tutors, setting up the views for these posts, and allowing customers to filter and view lessons by category.

    Ready to move on to the next part? Just let me know, or type “continue” to move on with the tutorial!

    Part 5: Implementing Lesson Posting and Viewing Functionality

    In this section, we will develop the functionality for tutors to post lessons within their chosen categories and enable customers to view available lessons. This will include setting up views and ensuring that posts can only be made by registered tutors.

    Specifications:

    • Post functionality for tutors.
    • Viewing functionality for customers to see available lessons.

    Steps:

    1. Modify Lessons Controller
      • Add actions in the Lessons controller to handle creating and listing lessons specific to categories:
      • Code snippet for create and index actions:
        • # app/controllers/lessons_controller.rb
          def index
            @lessons = Lesson.includes(:category).where(category_id: params[:category_id])
          end
          
          def new
            @lesson = Lesson.new
          end
          
          def create
            @lesson = current_tutor.lessons.build(lesson_params)
            if @lesson.save
              redirect_to @lesson, notice: 'Lesson was successfully created.'
            else
              render :new
            end
          end
          
          private
          
          def lesson_params
            params.require(:lesson).permit(:title, :description, :category_id)
          end
          
    2. Implement Authorization
      • Ensure that only registered tutors can post lessons. Use a before_action filter to check if the user is a tutor:
      • Code snippet:
        • before_action :authenticate_user!
          before_action :check_if_tutor, only: [:new, :create]
          
          private
          
          def check_if_tutor
            redirect_to root_path, alert: 'Access denied.' unless current_user.tutor?
          end
          
    3. Setup Views for Lessons
      • Create views for new and index to allow tutors to add lessons and customers to browse them:
      • Use Tailwind CSS to style the forms and listings to ensure they are visually appealing and functional.

    This setup not only allows tutors to manage their lessons but also enables customers to easily view available lessons by category, enhancing the functionality of our educational platform.

    Next Steps:

    Next, we will implement the booking functionality for lessons, including a calendar system where customers can book lessons from their preferred tutors based on available dates and times.

    Continuation Plan:

    The upcoming section will focus on integrating a calendar and multiple hour selection for booking, ensuring tutors can define their availability and customers can make bookings accordingly.

    1. Generate Category Model
      • Command:
        • rails generate model Category name:string parent_id:integer:index
          
    2. Set Up Category Self-Referential Association
      • Edit the Category model to include self-referential associations:
      • Code snippet:
        • class Category < ApplicationRecord
            has_many :subcategories, class_name: "Category", foreign_key: "parent_id"
            belongs_to :parent_category, class_name: "Category", optional: true
          end
          
    3. Generate Lesson Model
      • Command:
        • rails generate model Lesson title:string description:text tutor_id:integer:index category_id:integer:index
          
    4. Establish Relationships in Models
      • Modify Lesson and Tutor models to include associations:
      • Code snippet for Lesson model:
        • class Lesson < ApplicationRecord
            belongs_to :tutor
            belongs_to :category
          end
          
      • Code snippet for Tutor model update:
        • class Tutor < ApplicationRecord
            has_many :lessons
            belongs_to :user
          end
          
    5. Run Migrations
      • After setting up the models with their associations, execute the migrations to update the database:
      • Command:
        • rails db:migrate
          
    6. Create Controllers for Categories and Lessons
      • Create controllers to manage categories and lessons, allowing CRUD operations:
      • Commands:
        • rails generate controller Categories index new create edit update destroy
          rails generate controller Lessons index new create edit update destroy
          
    7. Implement Views for Category Management
      • Create views for each action in the Categories controller to allow users to create, edit, and view categories:
      • Ensure the views use forms that are appropriate for nested categories, possibly using JavaScript to enhance user interaction.
    8. Setup Routes
      • Define the necessary routes in config/routes.rb for handling category and lesson management:
      • Code snippet:
        • resources :categories do
            resources :lessons
          end
          

    This section has set up a robust system for managing lessons and categories, allowing tutors to categorize their lessons effectively and customers to browse lessons according to these categories.

    Next Steps:

    Next, we will develop the functionality for tutors to post lessons within their chosen categories and for customers to view available lessons. We’ll also add post functionality and category selection for tutors.

    Continuation Plan:

    The next part will cover integrating the post functionality for tutors, setting up the views for these posts, and allowing customers to filter and view lessons by category.

    Ready to move on to the next part? Just let me know, or type “continue” to move on with the tutorial!

    Part 5: Implementing Lesson Posting and Viewing Functionality

    In this section, we will develop the functionality for tutors to post lessons within their chosen categories and enable customers to view available lessons. This will include setting up views and ensuring that posts can only be made by registered tutors.

    Specifications:

    • Post functionality for tutors.
    • Viewing functionality for customers to see available lessons.

    Steps:

    1. Modify Lessons Controller
      • Add actions in the Lessons controller to handle creating and listing lessons specific to categories:
      • Code snippet for create and index actions:
        • # app/controllers/lessons_controller.rb
          def index
            @lessons = Lesson.includes(:category).where(category_id: params[:category_id])
          end
          
          def new
            @lesson = Lesson.new
          end
          
          def create
            @lesson = current_tutor.lessons.build(lesson_params)
            if @lesson.save
              redirect_to @lesson, notice: 'Lesson was successfully created.'
            else
              render :new
            end
          end
          
          private
          
          def lesson_params
            params.require(:lesson).permit(:title, :description, :category_id)
          end
          
    2. Implement Authorization
      • Ensure that only registered tutors can post lessons. Use a before_action filter to check if the user is a tutor:
      • Code snippet:
        • before_action :authenticate_user!
          before_action :check_if_tutor, only: [:new, :create]
          
          private
          
          def check_if_tutor
            redirect_to root_path, alert: 'Access denied.' unless current_user.tutor?
          end
          
    3. Setup Views for Lessons
      • Create views for new and index to allow tutors to add lessons and customers to browse them:
      • Use Tailwind CSS to style the forms and listings to ensure they are visually appealing and functional.

    This setup not only allows tutors to manage their lessons but also enables customers to easily view available lessons by category, enhancing the functionality of our educational platform.

    Next Steps:

    Next, we will implement the booking functionality for lessons, including a calendar system where customers can book lessons from their preferred tutors based on available dates and times.

    Continuation Plan:

    The upcoming section will focus on integrating a calendar and multiple hour selection for booking, ensuring tutors can define their availability and customers can make bookings accordingly.


    Posted

    in

    ,

    by

    Tags: