Day 2: Building a simple ToDo application

In order to keep things simple, we’re going to create a simple todo list application. This application should display a list of tasks. We should be able to add new tasks. Delete and edit existing tasks. Also, there should be a way to mark a task as finished.

rails new

We will start it off by executing several commands in the Terminal. The first command is rails new. It will generate us a skeleton for our new app.

$ cd ~
$ rails new todo --database=postgresql

It will take some time to generate files and install dependencies. After it’s done, you should see our newly generated project in the folder todo. Let’s go into this folder and setup the database.

$ cd todo
$ bundle exec rake db:create db:migrate db:schema:load
$ bundle exec rails server

That’s it! Four commands and we have a running website. Open up your browser, go to http://localhost:3000 and you should see the Ruby on Rails welcome page.

Default Ruby on Rails page

Let me explain what happened here. First, we used the command called cd. It stands for “change directory”. It jumps to a directory you specified. Second command consist of 3 parts.

bundle exec rials db:migrate explained

  1. bundle exec tells a terminal to use libraries installed inside a current project. There might be a different version of rake installed but we tell it to use the one installed for our project.
  2. rake is a helper utility. It does a lot of helpful stuff. In our case it creates a new database. It also migrates the database so it has a proper schema.
  3. db:create db:migrate db:schema:load are the three tasks that we provide rake with. You can specify one or many tasks separated by a space.

There are many useful rake tasks. If you want to see a full list of them, call rake with -T option.

$ bundle exec rake -T
rake about              # List versions of all Rails frameworks and the environment
rake assets:clean[keep] # Remove old compiled assets
...

Note: For keeping it simple, just prefix every command with bundle exec if you’re inside your project directory.

Everything that we’re going to run inside the terminal will consist of these three basic parts.

bundle exec someutility parameters

The last command that we ran is no exception. bundle exec rails server starts up a server locally.

Create, Edit, Delete tasks

We got our first rails app running in just four commands. How awesome it is? Now let’s add some features to our application.

First, we want to be able to create, edit and delete our tasks. Rails has a code generator for exactly this purpose. It’s called scaffold generator. Let’s give it a shot. Go back to your terminal. I recommend to leave the server running in the current window and open a new terminal (Command-T if you’re on a mac).

$ cd ~/todo
$ bundle exec rails generate scaffold task name:string
$ bundle exec rake db:migrate

Go back to your browser and go to http://localhost:3000/tasks

Ruby on Rails scaffold generator

This is a task listing. It looks very lonely, since there is no tasks. Let’s add a new task. Click on the New Task link. It should take you to the New Task screen.

Ruby on Rails scaffold for new

Enter “Learn how to program” in the Name field and click the Create Task button. Now you should see the screen representing our newly created task.

Ruby on Rails scaffold for show

Click the “Back” link and you should see the Task Listing screen with our newly created task. Yay!

Tast listing

You see how easy it is to create a website with Ruby on Rails? Now that we a website running locally, let’s dive deeper. We’ll see how Rails works under the hood.

Photo by purpleslog