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.
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
-
-
- gem 'rails', '~> 5.1.1'
-
-
-
- gem 'sqlite3'
-
-
-
- gem 'puma', '~> 3.7'
-
-
-
- gem 'sass-rails', '~> 5.0'
-
-
-
- gem 'uglifier', '>= 1.3.0'
-
-
-
- gem 'coffee-rails', '~> 4.2'
-
-
-
- gem 'turbolinks', '~> 5'
-
-
-
- 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.
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.