Learning Rails Pt. 2

Mar 19, 2023

Open the http://localhost:3000/about in the web browser. Obviously, you get the No route matches [GET] "/about" error. Because, this route doesn’t exist.

The routes for the Rails application are defined in config/routes.rb file. Open this routes.rb file in the text editor. You should see the following code.

Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
  # Can be used by load balancers and uptime monitors to verify that the app is live.
  get "up" => "rails/health#show", as: :rails_health_check

  # Defines the root path route ("/")
  # root "posts#index"
end

Remove everything within do...end block.

Rails.application.routes.draw do

end

Add the /about route line like this.

Rails.application.routes.draw do
  get "/about" => "about#index"
end

This line means, when GET /about is called, run the about’s index method. This about is a controller (or class) and index is an action (or method within class).

Reload the /about URL in the browser. You should now get uninitialized constant AboutController error. Because, we do not have AboutController (CamelCase) in about_controller.rb (snake_case) file.

All the controllers for the Rails application are defined within app/controllers folder. In app/controllers, create a file with name about_controller.rb.

touch app/controllers/about_controller.rb

Open this file in text editor and write the following code.

class AboutController < ApplicationController
end

Reload the /about in the browser again. You should now get The action 'index' could not be found for AboutController error. Because, AboutController does not have index action.

Let’s define an empty index method in this controller like this.

class AboutController < ApplicationController
  def index
  end
end

Reload the /about in the browser for one more time. You should now get AboutController#index is missing a template for request error. Below it, you’ll see an example description on what Rails is looking for. We are getting this error because, we do not have index.html.erb template or view for the method to fulfil the request.

All the views for the Rails application are defined within app/views folder. Create a file with name index.html.erb in the folder with name about inside the app/views folder. Here, we need to create about folder first and then index.html.erb file in this created about folder.

mkdir app/views/about
touch app/views/about/index.html.erb

Open this file in the text editor and write the following code.

<h1>About</h1>

Reload the /about in the browser for the last time. You should see the About header in the browser now!

In summary, Rails check for the routes in routes.rb file and run the associated action from given controller. Finally, with the help from associated view, request will be fulfilled.

Tags: rails