
- Ruby on Rails - Home
- Ruby on Rails - Introduction
- Ruby on Rails - Installation
- Ruby on Rails - IDEs
- Ruby on Rails - Hello World
- Ruby on Rails - Framework
- Ruby on Rails - Directory Structure
- Ruby on Rails - Rails Console
- Ruby on Rails - Bundler
- Ruby on Rails - Examples
- Ruby on Rails - Database Setup
- Ruby on Rails - Active Records
- Ruby on Rails - Validation
- Active Record Associations
- Active Record Query
- Ruby on Rails - Migrations
- Ruby on Rails - Active Model
- Ruby on Rails - Controllers
- Cookies and Session
- Ruby on Rails - Authentication
- Ruby on Rails - Routes
- Ruby on Rails - Views
- Ruby on Rails - Rendering
- Ruby on Rails - Layouts
- Ruby on Rails - Scaffolding
- Ruby on Rails - Forms
- Ruby on Rails - Active Jobs
- Ruby on Rails - Action Text
- Ruby on Rails - Active Storage
- Ruby on Rails - JavaScript
- Ruby on Rails - Propshaft
- Ruby on Rails - ImportMap
- Ruby on Rails - AJAX
- Ruby on Rails - WebSockets
- Ruby on Rails - Action Cable
- Ruby on Rails - File Uploading
- Ruby on Rails - Send Emails
- Ruby on Rails - Rack
- Ruby on Rails - Error Handling
- Ruby on Rails - Deployment
- Ruby on Rails Resources
- Ruby on Rails - References Guide
- Ruby on Rails - Quick Guide
- Ruby on Rails - Resources
- Ruby on Rails - Discussion
- Ruby Tutorial
- Ruby Tutorial
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.
