Tutorial Day 1: Create a Ruby on Rails 4 application

Hi guys,

Here's a series of tutorials on creating a Rails application from scratch.
It is addressed to Rails newcomers.
Code can be found at github: kangaroute source code & the demo app: kangaroute demo
By following the instructions, you'll learn to create a Rails application, use Git, test your code and some tips.

What does the application?

Kangaroute is a car pooling application. Users submit routes with departure and arrival data either as drivers or as passenger and the application finds routes that can be combined.

So, let's start! (Estimated time: 45min)

1 - Install RVM & Ruby 2.1.1

First of all, you need Ruby lang installed. A nice & easy way to install multiple versions of Ruby is RVM. You can check if RVM is already installed with rvm -v. If you don't see the RVM version, here's the installation guide.

After having RVM installed, check if Ruby is installed with rvm list. If no ruby was listed, install the latest version: rvm install 2.1.1 and tell RVM to use this version rvm use ruby-2.1.1

2 - Install Rails and create a new app

RVM organises all gems needed in a project in sets called gemsets. You need to create a new gemset and install rails gem. Here's how:rvm gemset create kangaroute, rvm gemset use kangaroute, gem install rails.

Tip: Let's say to our computer which ruby and rails versions we'll use for kangaroute once and for all.
In kangaroute directory create the following files:
1. Create .ruby-version file and inside it write ruby-2.1.1
2. Create .ruby-gemset and inside put kangaroute
From now on, every time you get into kangaroute directory ruby and rails versions will be automatically selected.

Now that you have Rails installed, it's time to create your fresh Rails application. Run rails new kangaroute. This command creates a set of files structured according to the MVC architectural pattern.

3 - Always use version control

Now, it's time to make safe what we've done so far. Let's use git!
First, get with your terminal inside your new app cd kangaroute and run git init to initialize git, git add -A to add all files in kangaroute directory and git commit -m 'Initial commit' to save the current version of all files included in this directory.

4 - Database schema

Let's create the core entity of this application which is Route. Route will have the following fields in the database: kangaroute database schema

5 - PostgreSQL db & scaffold generator

We'll use PostgreSQL database so in the Gemfile replace line gem 'sqlite3' with gem 'pg' and run bundle.
Rails stores all database related information at /config/database.yml, so change it like this: database.yml and don't forget to change username & password.

According to the above database schema, generate the following scaffold to create Route:
rails g scaffold Route departure_address:string arrival_address:string 'departure_lat:decimal{18,12}' 'departure_lng:decimal{18,12}' 'arrival_lat:decimal{18,12}' 'arrival_lng:decimal{18,12}' departure_distance_range:float arrival_distance_range:float start_time:datetime time_range_from:datetime time_range_to:datetime active:boolean user_id:integer:index.
Don't get scared, try to read it. It's that huge because we have a lot of fields!
Scaffold will create for us a bunch of files like model, controller, views, migration, tests etc. What a time savior!

6 - Wrap up

Let's create database and routes table in it and then we'll call it a day.
Run rake db:create db:migrate and voila!
To see what we've made so far, run a server:rails s and with your favorite browser (eg Chrome) visit http://localhost:3000/routes and http://localhost:3000/routes/new
It doesn't look like a masterpiece I guess but that's just the beginning!!

Stay tuned for Day 2!

Thanks for reading and sharing! ;)

comments powered by Disqus