Chapter 12: Final Capstone Project – Build a Task Manager

Chapter 12: Final Capstone Project – Build a Task Manager

AI Reading

Quick summary of this article

This final chapter guides you through building a complete Task Manager web application using Ruby on Rails, bringing together all the skills you've learned throughout the course. The project lets signed-in users create, edit, complete, and delete their own personal tasks, with each user only seeing their own data.

  • Start by creating a new Rails app and adding Devise for user authentication, then generate a Task scaffold with title, description, and completed fields.
  • Connect tasks to users by adding a belongs_to :user association in the Task model and a has_many :tasks in the User model, along with a migration to add the user reference.
  • Protect the app by requiring login with authenticate_user! and scoping all task queries to current_user.tasks, so users can only access their own tasks.
  • Add a validation to require a task title, and test the app by running rails test and then starting the server with rails server.
  • After completing the project, you can deploy it to platforms like Render or Heroku, add styling with Bootstrap or Tailwind, and continue building small projects to master Rails.

Chapter 12: Final Capstone Project – Build a Task Manager

Congratulations, you have reached the final chapter! It is time to put everything you have learned together: creating an app, routing, controllers, views, ERB, models, migrations, associations, validations, and authentication. Your capstone project is a Task Manager where signed-in users can create, edit, complete, and delete their own tasks.

🚧 Step 1: Create the App

rails new task_manager
cd task_manager

💾 Step 2: Add Authentication

Add Devise and create the User model, exactly like the previous chapter:

# Gemfile
gem "devise"
bundle install
rails generate devise:install
rails generate devise User
rails db:migrate

🎨 Step 3: Generate the Task Resource

Generate a scaffold for tasks with a title, description, and a completed checkbox:

rails generate scaffold Task title:string description:text completed:boolean
rails db:migrate

🔗 Step 4: Connect Tasks to Users

Add the association so every task belongs to a user:

# app/models/user.rb
class User < ApplicationRecord
  has_many :tasks, dependent: :destroy
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
end
# app/models/task.rb
class Task < ApplicationRecord
  belongs_to :user
  validates :title, presence: true
end

Add a user_id column to tasks with a migration:

rails generate migration AddUserToTasks user:references
rails db:migrate

🔧 Step 5: Protect and Personalize

Require login and make sure a user only sees their own tasks:

# app/controllers/tasks_controller.rb
class TasksController < ApplicationController
  before_action :authenticate_user!
  before_action :set_task, only: %i[show edit update destroy]

  def index
    @tasks = current_user.tasks
  end

  def create
    @task = current_user.tasks.new(task_params)
    if @task.save
      redirect_to @task, notice: "Task was successfully created."
    else
      render :new, status: :unprocessable_entity
    end
  end

  # ... update, destroy, set_task, task_params ...

  private

  def set_task
    @task = current_user.tasks.find(params[:id])
  end

  def task_params
    params.require(:task).permit(:title, :description, :completed)
  end
end

🏠 Step 6: Run and Test

rails test
rails server

Open http://localhost:3000, sign up, and start creating tasks. Each user only sees their own tasks, and you can mark tasks as completed by checking the box. Try creating a task without a title and you will see the validation message.

🎉 Congratulations

You have built a complete, secure web application with Ruby on Rails! Along the way you learned the MVC pattern, routing, controllers, views, ERB, models, migrations, Active Record, CRUD, associations, validations, and authentication. You now have a solid foundation to build almost any web application.

What to explore next: deploy your app to Render or Heroku so the world can use it, add a friendly UI with Bootstrap or Tailwind, write feature tests with Capybara, and keep building small projects. The best way to master Rails is to keep building. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *