Create your first Rails Application

ADMAT Bandara
5 min readOct 20, 2017

I write this article to give an idea on creating a rails application from the beginning. This article will be more suitable for beginners with some programming knowledge.

What is Rails ?

Rails is a web application framework written in Ruby Language. Programming web application will become easier from this.

I am developing rails application since last year and according to my experience I write less code to accomplish some task when it compares to other web programming languages. (I have tried J2EE and PHP). If you want to study Rails, there is nice guide for you. That;s the official guide. Check it using the following link.

And to develop rails applications you should have some knowledge about Ruby as well. Ruby is the heart 💓 of Rails.

Use the following link to learn ruby.

How to setup the computer for Rails

First we need to setup the computer in order to program ruby on rails. I am using Ubuntu 16.04 LTS for this. I am using the terminal coming with RubyMine 2017 and Atom as the editor.

  1. Install Ruby
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev nodejs

2. You will need rbenv when you want switch between ruby versions. So install rbenv

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

rbenv install 2.3.0
rbenv global 2.3.0

you can check the installed ruby version

ruby -v
My PC ruby version

3. Install Bundler

Bundler is the best way to handle and manage ruby gems.

gem install bundler

4. Add Node JS Dependencies

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

5. Install Rails

Rails is actually a gem. So we install it like a gem

gem install rails -v 4.2.3

Latest version is 5.0.0 now.

6. Run the following command to make the rails executable available

rbenv rehash

Let’s create the first application

  1. Run the create command to create rails application
      version     name of the application
rails _4.2.3_ new myapp

here you can choose the database type you need as well. (MySQL, SQLlite, PostgreSQL).

rails _version_ new AppName -d DB_TYPErails _4.2.3_ new MyFirstApp -d mysql

If you didn’t put -d in the code rails will create the application with SQLlite default.

When you run this command in the URL

2. Go to the created Folder

cd MyFirstApp

3. Run Bundle install to complete installing gem files

bundle install

you should get the successful message like this.

Bundle complete! 12 Gemfile dependencies, 61 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

4. Now I will open my folder via Atom for the development.

atom .

Above command will work only if you have install the atom correctly

5. Create the database according to the config database file.

default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
development:
<<: *default
database: MyFirstApp_development
test:
<<: *default
database: MyFirstApp_test
production:
<<: *default
database: MyFirstApp_production
username: MyFirstApp
password: <%= ENV['MYFIRSTAPP_DATABASE_PASSWORD'] %>

Above is the database.yml file under the config directory. If you have a MySQL password you enter it here and at the same time create a database with this name in your MySQL console.

6. Run the Rails Server

rails serverorrails s

7. Open the Browser and Enter the URL

localhost:3000

Initially it should look like this

Done ! , Now we have successfully created our first application using Ruby on Rails.

After this you can create controllers, view needed to develop the application useful.

This Rails application has MVC structure. If you want to read about MVC in rails I recommend the following article.

--

--