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.
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.
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.
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.
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 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.
- 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.
- 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.
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).
Go back to your browser and go to http://localhost:3000/tasks
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.
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.
Click the “Back” link and you should see the Task Listing screen with our newly created task. Yay!
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