Ruby on Rails, often referred to simply as Rails, is a powerful and popular web application framework known for its elegant and developer-friendly design. Developed by David Heinemeier Hansson in 2005, Rails has gained a devoted following due to its simplicity and convention over configuration philosophy. If you’re new to Rails and want to dip your toes into web development, this “Hello World” tutorial will guide you through the process of creating a basic Rails application.
Prerequisites
Before diving into the Rails world, make sure you have the following prerequisites:
- Ruby: Rails is built on the Ruby programming language. You’ll need to have Ruby installed on your system. You can check your Ruby version by running
ruby -v
in your terminal. If it’s not installed, follow the instructions for your specific operating system from ruby-lang.org. - RubyGems: RubyGems is the package manager for Ruby. It’s usually bundled with Ruby, but you can check if it’s available by running
gem -v
. If it’s not installed, you can install it by following the instructions on the RubyGems website. - SQLite (optional): Rails uses SQLite as the default database. It should be installed by default on most systems. However, if you plan to use a different database like MySQL or PostgreSQL, make sure it’s installed and correctly configured.
- A Text Editor: You’ll need a code editor to write your Rails application. Some popular choices include Visual Studio Code, Sublime Text, and Atom.
Installing Rails
Now that you’ve got Ruby and RubyGems installed, it’s time to install Ruby on Rails. Open your terminal and run the following command:
gem install rails
This command will download and install the latest version of Rails. After the installation is complete, you can verify it by running:
rails -v
You should see the version number displayed in your terminal, confirming a successful installation.
Creating Your First Rails Application
With Rails installed, you’re ready to create your first Rails application. Rails provides a powerful tool called rails new
that generates the basic structure for a new project. Let’s create a simple “Hello World” application:
- Open your terminal and navigate to the directory where you want to create your Rails application. Use the
cd
command to change directories. - Run the following command to create a new Rails application called “HelloWorldApp”:
rails new HelloWorldApp
This command will generate the necessary files and directories for your Rails application.
- After the generation process is complete, navigate to your application’s directory:
cd HelloWorldApp
Writing Your First Controller and View
In a Rails application, the logic is divided into controllers and views. Controllers handle requests and send data to views for presentation. Let’s create a simple “Hello, World!” message.
- Generate a new controller named “Welcome” with an action (a method) called “index” using this command:
rails generate controller Welcome index
This command creates a new controller file and an associated view.
- Open the
app/controllers/welcome_controller.rb
file in your text editor. You’ll see aWelcomeController
class with anindex
action. Modify it to look like this:
class WelcomeController < ApplicationController
def index
@message = "Hello, World!"
end
end
Here, we’ve defined a variable @message
with the message we want to display.
- Now, navigate to the
app/views/welcome
directory and open theindex.html.erb
file in your text editor. Replace its content with the following code:
<h1><%= @message %></h1>
This code is an ERB (Embedded Ruby) template that will display the message stored in the @message
variable.
Configuring Routes
In Rails, routes determine how incoming requests are routed to the appropriate controller actions. Let’s configure a route to our WelcomeController#index
action.
- Open the
config/routes.rb
file in your text editor. Add the following line to define a root route:
root 'welcome#index'
This tells Rails to route the root URL to the WelcomeController
‘s index
action.
Starting the Server
Now that you’ve created your “Hello World” application, it’s time to start the development server:
rails server
You’ll see some output in your terminal, including a message indicating that the server is running. By default, your Rails application will be accessible at http://localhost:3000
.
Open your web browser and navigate to that URL, and you should see the “Hello, World!” message displayed on the page.
Congratulations! You’ve just built your first Ruby on Rails application. You’ve learned how to create a basic controller, view, and route. This is just the beginning of your journey into the world of Rails, a powerful and flexible framework for building web applications.
From here, you can explore more advanced features, such as creating models and working with databases, implementing user authentication, and building interactive web applications with the rich ecosystem of Ruby on Rails. Happy coding!
Leave a Reply