Ruby on Rails - Hello World



To get the "Hello world" message displayed in the browser with a Rails application, you need to create a route, a controller with an action and a view.

  • A route maps a request to a controller action. In Rails, a route is the part of the URL that determines how an incoming HTTP request is directed to the appropriate controller and action for processing.
  • A Controller is a Ruby class, which inherits from ApplicationController class, and its public methods are actions.
  • Views in Rails are templates, usually written in a mixture of HTML and Ruby. They are saved with the .erb extension (stands for embedded Ruby).

Rails Generate Command

Rails comes with a powerful command line tool called the generate command. It can be used for different purposes. Different types of generators will be displayed with the following command −

>rails generate
Usage:
  bin/rails generate GENERATOR [args] [options]

General options:
  -h, [--help]     # Print generator's options and usage
  -p, [--pretend]  # Run but do not make any changes
  -f, [--force]    # Overwrite files that already exist
  -s, [--skip]     # Skip files that already exist
  -q, [--quiet]    # Suppress status output

Please choose a generator below.

Rails:
  application_record
  authentication
  benchmark
  channel
  controller
  generator
  helper
  integration_test
  jbuilder
  job
  mailbox
  mailer
  migration
  model
  resource
  scaffold
.
.
.

Rails Generate Controller Command

Rails generators are built on top of Thor, a simple and efficient tool for building selfdocumenting command line utilities. Let us generate a Controller for our application.

The syntax of the rails generate controller command is as follows −

rails generate controller ControllerName [action1 action2 ...] [options]

In this syntax −

  • ControllerName − The name of the controller you want to create.
  • [action1 action2 ...] − A space-separated list of actions (methods) you want to define in the controller.

For each action, Rails will generate corresponding view files in the appropriate directory.

Open your terminal, navigate to your Rails project directory, and run the following command to create a controller named index and an action named index.

rails generate controller index index

This command generates −

  • The controller itself
  • A views folder for the controller
  • A view file for the specified actions
  • A test file for this controller
  • A helper file for extracting logic in our views

The following activity will appear in the terminal window −

create  app/controllers/index_controller.rb
      route   get "index/index"
      invoke  erb
      create  app/views/index
      create  app/views/index/index.html.erb
      invoke  test_unit
      create  test/controllers/index_controller_test.rb
      invoke  helper
      create  app/helpers/index_helper.rb
      invoke  test_unit

In Ruby on Rails, an action in a controller is essentially a method defined within the controller class.

Edit the index_controller.rb file to include the definition of index action as follows −

class IndexController < ApplicationController
  def index
  end
end

We now need to provide a template to be rendered by the index action. Rails assumes that the template is named as index.html and attaches .erb as its extension. This file embeds Ruby code inside HTML script.

Open a new file in the editor. Save the following HTML code and save it as index.html.erb

<h1 style="text-align: center;">Hello World</h1>

To tell Rails the root route should render the index action, open the config/routes.rb file and define a route for the root URL (/). Modify it to look like this −

Rails.application.routes.draw do
  root "index#index"
end

Run the Rails Server

Rerun the Rails server if not already running, and visit the http://localhost:3000 URL, instead of the Rails welcome page, you should get the Hello World message displayed in the browser.

Run the Rails Server
Advertisements