Create a Rails App
Overview
Make sure have installed Ruby and Rails.
To create a Rails application, run the following command in your terminal:
rails new test_rails_app
If you have a specific version that you want the Rails app to use, you can do:
rails _<version-number> new test_rails_app
# Example:
rails _5.2.1.4_ new test_rails_app
After your application is created, switch to its directory:
cd test_rails_app
To start the server:
rails server
# Another way
rails s
Open a web browser and navigate to:
http://localhost:3000
The default Rails welcome page is displayed. This confirms the application is running and the root route is active.

To stop the server, press Ctrl+C in the terminal.
For now, we'll keep the server running so we can simply refresh the page when we introduce configuration changes in the next steps.
Directory Structure
The main directories in a Rails app are:
test_rails_app/app
├── assets
├── controllers
├── helpers
├── javascript
├── jobs
├── mailers
├── models
└── views
These directories separate logic, data, and presentation, which keeps the application organized as it grows.
Inside the controllers directory, application_controller.rb serves as the base controller and will be used in the next step to control how the root route responds.
test_rails_app/app/controllers/
├── application_controller.rb
└── concerns
Root Route
The root route is the default page shown when no extra path is provided.
- It is the base URL of the application
- It handles the first incoming request
- It can be customized in the routes file
For example:
http://example.comhttp://localhost:3000
This route is what users see first, so changing it lets us control the initial page of the application.
- Routes are defined in
config/routes.rb - Where each route maps a request to a controller action.
- Root route is usually the starting point for this flow.
To update the route, edit the config/routes.rb:
Rails.application.routes.draw do
root "application#hello"
get "up" => "rails/health#show", as: :rails_health_check
end
This tells Rails to send the root request to the ApplicationController and handle it using the hello method for that controller.
This Hello method doesn't actually exist yet, and will be created in the next step.
Application Controller
The application controller is the base controller for the entire app.
- Other controllers inherit from it
- Shared behavior is defined here
- It can directly handle routes if needed
Open app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base
allow_browser versions: :modern
stale_when_importmap_changes
end
The class definition ApplicationController inherits from ActionController, which gives it core controller features such as handling requests and rendering responses.
This line limits supported browsers to modern versions:
allow_browser versions: :modern
This line helps Rails manage caching when JavaScript import maps change:
stale_when_importmap_changes
To define a hello method, edit app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base
allow_browser versions: :modern
stale_when_importmap_changes
def hello
render html: "Hello, world!"
end
end
Back in the web browser, refresh the page. The page now displays "Hello world!", which confirms that the root route and controller action are working as expected.
Note that this approach does not follow Rails conventions. The HTML should not be defined in the application controller, but instead placed inside .html.erb files within the views directory, where presentation logic belongs.
Conventional Expectations
Rails follows "convention over configuration," which means it expects code and files to follow standard patterns.
- Define a route pointing to
controller#action - Create a controller with a meaningful name
- Add an action/method in that controller
- Place the corresponding
.html.erbtemplate in the matching folder under views
For example, for static pages:
- Controller:
pages_controller - Action:
home
Rails will then expect:
app/views/pagesfolder matching the controller namehome.html.erbtemplate inside that folder matching the action name
These conventions keeps the app organized and allows Rails to automatically link routes, controllers, and views.
Go back to the app/controllers/application_controller.rb and remove the hello action to reset the application controller to only contain shared behavior.
class ApplicationController < ActionController::Base
allow_browser versions: :modern
stale_when_importmap_changes
end
Create a Controller
Controllers handle actions that respond to routes.
To create a new controller, run:
rails generate controller pages
This will create the following:
app/controllers/pages_controller.rbtemplateapp/views/pagesfolder
Next, update config/routes.rb to point the root route to the new controller and action:
Rails.application.routes.draw do
root "pages#home"
get "up" => "rails/health#show", as: :rails_health_check
end
Add a Controller Action
After creating the pages controller, the next step is to define a controller action to handle the root route.
Edit app/controllers/pages_controller.rb:
class PagesController < ApplicationController
def home
end
end
This prepares the controller to render a view automatically.
Create the View
Once the home action is defined in PagesController, next is to create its view so the content can be displayed in the browser.
To do this, create the app/views/pages/home.html.erb:
Hello, world!
Go back to the browser and refresh the page. The text "Hello, world!" now appears at http://localhost:3000.
How the Request Flows
Rails follows a clear pattern when handling requests.
- User sends a request from the browser
- Router receives the request
- Router determines the destination based on defined routes
- Router sends the request to the correct controller action
- If needed, the action interacts with models to fetch or save data
- Model returns data to the controller
- Controller selects the appropriate view template
- View renders the response and sends it back to the user
In this case:
- No database or models are involved
- The controller directly renders the view
This same pattern repeats throughout Rails applications and becomes the foundation for more complex features.