Skip to main content

Create your first Rails Application

What is Rails?

Rails is a web application framework written in Ruby Language. Programming web application will become easier by this. I am developing rails applications since last year and according to my experience I write less code to accomplish some tasks when compared to other web programming languages. (I have tried J2EE and PHP). If you want to study Rails, there is a 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 this 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

image
                                                     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. Latest version is 5.0.0 now.

gem install rails -v 4.2.3

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

rbenv rehash

Let’s create the first application

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_TYPE rails _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

image

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

image

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.

image

6. Run the Rails Server

rails server or rails s

image

7. Open the Browser and Enter the URL

localhost:3000

Initially it should look like this

image

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  this article.



22 / October / 2018 Ruby, Rails by Asitha Bandara

LEAVE A COMMENT







POST COMMENTS


© 2021 Creative Software. All Rights Reserved | Privacy | Terms of Use