Creating Applications In Ruby On Rails

In this article you will learn how to create simple applications in Ruby on Rails.

Introduction

Welcome to my Ruby on Rails article. I assume you have already read my previous article on introduction and installation of Ruby on Rails. Now, you will learn how to create a simple sample application in Ruby on Rails. We will learn about the application file structure, definitions, process of starting Rails Server and how to run the application in web browser.

Requirement 

  • Software - Ruby on Rails installed in your system.
  • Editor      - Atom text editor is the best in class.
  • Browser  - Google Chrome web browser.

Application creation

Step 1

Open the Ruby on Rails Command prompt by going to Start >> All Programs >> RailsInstaller >> Command Prompt with Ruby and Rails.

Step 2

After opening the command prompt, now let's create a new application in rails using the following code.

  1. rails new simple_app  

 

Step 3

Once you enter the code, it will take some time to create the app. All the supporting files and folders related to configuration, database, library, log, test, temp, vendor files etc. will be created automatically with this bundle command.

The gem mestadata will be fetched from rubygems.org. 

Step 4

The application is created successfully. Now, you can find the application installed at C:/Sites.

Also, you can explore the folder to learn about application file structure and definitions.

Application File Structure

App directory

App folder contains most of the application code. You spend most of your time writing the code in this app folder. There are some sub directories also in this folder, such as - Models, Views, and Controllers (MVC architecture ), and helpers to help us write code in our views.

  • Assets folder is used to store JavaScript, Stylesheets and some basic images.
  • Mailers folder is used if you want to send mails from your application.
  • Jobs is used for writing tasks that your application is going to do.

 

Config directory

This directory is used to configure the application. There is a lot of important default code already in there that we need to override so as to configure the application as per our requirement.

db directory

db is used for database. Here, all your database files are saved.

Gemfile

Here you get the informationb of what RubyGems in your application is loaded.

Example code

  1. # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'  
  2.   
  3. gem 'rails''~> 5.1.1'  
  4.   
  5. # Use sqlite3 as the database for Active Record  
  6.   
  7. gem 'sqlite3'  
  8.   
  9. # Use Puma as the app server  
  10.   
  11. gem 'puma''~> 3.7'  
  12.   
  13. # Use SCSS for stylesheets  
  14.   
  15. gem 'sass-rails''~> 5.0'  
  16.   
  17. # Use Uglifier as compressor for JavaScript assets  
  18.   
  19. gem 'uglifier''>= 1.3.0'  
  20.   
  21. # Use CoffeeScript for .coffee assets and views  
  22.   
  23. gem 'coffee-rails''~> 4.2'  
  24.   
  25. # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks  
  26.   
  27. gem 'turbolinks''~> 5'  
  28.   
  29. # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder  
  30.   
  31. gem 'jbuilder''~> 2.5'  
 
 
Log directory

Here, the log files are stored. This is useful for tracking app errors and finding out the cause of a problem. 

Test directory

If you write test codes using Respec, that code can be inside this folder. 

This is your basic file directory and you are going to spend a lot of time inside app, inside config, and sometimes inside db.

Run the application on Server

Step 1

Before you start the Server, you must enter the application folder by using simple cd/ command. Then, start the Rails Server using this code in Ruby on Rails command prompt. 

  1. rails s  

 

 

After you start Rails server, the Puma Server application starts booting the server. Your application starts development on http://localhost:3000
 
Note -  Don’t close the Ruby on Rails command prompt while the Server is running. If you close the Command prompt, the webpage will stop working. so, simply minimize the command prompt.

 
Step 2

Now, you can check into your browser and search http://localhost:3000 . The application will start with default webpage.



Step 3

Now, you can exit the Server by using this key in Ruby on Rails command prompt (Ctrl+C).

Conclusion

I hope you have understood how to create Ruby on Rails applications, and how to start and terminate the Rails Server. Stay tuned for learning more  about Ruby on Rails.