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

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.

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. gem 'rails', '~> 5.1.1'
  3. # Use sqlite3 as the database for Active Record
  4. gem 'sqlite3'
  5. # Use Puma as the app server
  6. gem 'puma', '~> 3.7'
  7. # Use SCSS for stylesheets
  8. gem 'sass-rails', '~> 5.0'
  9. # Use Uglifier as compressor for JavaScript assets
  10. gem 'uglifier', '>= 1.3.0'
  11. # Use CoffeeScript for .coffee assets and views
  12. gem 'coffee-rails', '~> 4.2'
  13. # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
  14. gem 'turbolinks', '~> 5'
  15. # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
  16. 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.