Building web applications with Ruby on Rails

Ruby on Rails is a popular web application framework that allows developers to build web applications quickly and efficiently using the Ruby programming language. In this tutorial, we will cover the basics of building a web application using Ruby on Rails. This tutorial assumes that you have basic knowledge of Ruby programming language and web development.

Setting Up the Development Environment

Before we start building our application, we need to set up our development environment. Follow these steps to set up your development environment.

Install Ruby

First, we need to install Ruby on our machine. Ruby is available on multiple platforms, and you can download it from the official website https://www.ruby-lang.org/en/downloads/.

Install Rails

Once you have installed Ruby, we need to install Rails. Open your terminal and enter the following command to install Rails.

gem install rails

Verify Installation

After installing Ruby and Rails, we need to verify their installation to ensure that everything is working as expected.

To verify Ruby installation, open your terminal and enter the following command.

ruby --version

This command will display the version of installed Ruby.

To verify the Rails installation, enter the following command.

rails --version

This command will display the version of installed Rails.

If everything is working correctly, you should see the version of installed Ruby and Rails.

Creating a New Rails Application

Now that we have our development environment set up, we can create a new Rails application. Rails provides a command-line tool, rails, which we can use to create a new application.

Open your terminal and navigate to the directory where you want to create your application. Then, enter the following command to create a new Rails application.

rails new myapp

This command will create a new Rails application in a directory called myapp. It will also install all the necessary dependencies.

Once the command has completed, navigate to the newly created directory.

cd myapp

Understanding the Rails File Structure

Before we start building our application, let’s take a look at the file structure of a Rails application. Understanding the structure will help us navigate through the different files and directories in our application.

myapp/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ assets/
โ”‚   โ”‚   โ”œโ”€โ”€ stylesheets/
โ”‚   โ”‚   โ””โ”€โ”€ javascripts/
โ”‚   โ”œโ”€โ”€ controllers/
โ”‚   โ”œโ”€โ”€ helpers/
โ”‚   โ”œโ”€โ”€ mailers/
โ”‚   โ””โ”€โ”€ views/
โ”œโ”€โ”€ bin/
โ”œโ”€โ”€ config/
โ”‚   โ”œโ”€โ”€ initializers/
โ”‚   โ”œโ”€โ”€ locales/
โ”‚   โ”œโ”€โ”€ application.rb
โ”‚   โ”œโ”€โ”€ boot.rb
โ”‚   โ”œโ”€โ”€ database.yml
โ”‚   โ”œโ”€โ”€ environment.rb
โ”‚   โ””โ”€โ”€ routes.rb
โ”œโ”€โ”€ db/
โ”œโ”€โ”€ lib/
โ”œโ”€โ”€ log/
โ”œโ”€โ”€ public/
โ”œโ”€โ”€ test/
โ”œโ”€โ”€ tmp/
โ”œโ”€โ”€ vendor/
โ”œโ”€โ”€ Gemfile
โ””โ”€โ”€ README.md

Here is the description of some of the directories and files:

  • app: This directory contains the core functionality of our application, including controllers, models, and views.
  • bin: This directory contains executable files related to our application.
  • config: This directory contains configuration files for our application, including database configuration, application configuration, and routes.
  • db: This directory contains database-related files, including database schema, migrations, and seeds.
  • public: This directory contains static files, including images, JavaScript, and CSS.
  • test: This directory contains tests for our application.
  • vendor: This directory contains third-party code that we have added to our application.
  • Gemfile: This file specifies the required gems for our application.
  • README.md: This file contains instructions on how to use our application.

Creating a Simple Web Application

Now, let’s create a simple web application using Rails. In this example, we will create a web page that displays “Hello, World!”

Creating a Controller

To create a new controller, open your terminal and enter the following command:

rails generate controller welcome

This command will generate a new controller called Welcome in the app/controllers directory. It will also generate a few other files, including a view and a stylesheet.

Next, open the app/controllers/welcome_controller.rb file and add the following code:

class WelcomeController < ApplicationController
  def index
    render plain: 'Hello, World!'
  end
end

This code defines a new action called index that renders a plain text message, “Hello, World!”.

Creating a Route

Now that we have a controller and an action, we need to create a route so that Rails knows how to route a request to our action.

Open the config/routes.rb file and add the following code:

Rails.application.routes.draw do
  root 'welcome#index'
end

This code defines a new route that maps the root of our application to the index action of the welcome controller.

Starting the Server

Before we can view our web page, we need to start the Rails server. Open your terminal and enter the following command:

rails server

This command will start the server, and you should see the following message:

=> Booting Puma
=> Rails 6.1.3.2 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.2.2 (ruby 2.7.3-p183) ("Fettisdagsbulle")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 12724
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop

Viewing the Web Page

Open your web browser and navigate to `http://localhost:3000`. You should see a web page that displays “Hello, World!”.

Congratulations! You have created a simple web application using Ruby on Rails.

Conclusion

In this tutorial, we have covered the basics of building a web application using Ruby on Rails. We have set up our development environment, created a new Rails application, and built a simple web page that displays “Hello, World!”.

There is much more to learn about Ruby on Rails, including database integration, authentication, and deployment. Fortunately, Rails has a robust community and extensive documentation, making it easy to learn and build powerful web applications.

Related Post