[ToDoList] Introduction to Routing

  1. Basic Routes

 


 

As moving and seminal as the Rails welcome page is, we probably want something a bit more personal as our homepage (site root). This can be quickly achieved by editing just two files that already exist in our app, starting with /config/routes.rb:

config/routes.rb
Rails.application.routes.draw do get "up" => "rails/health#show", as: :rails_health_check root 'pages#hello' ## in controller#method format (snake_case) end
This will send all requests to the root (homepage) of the site to the #hello method in the PagesController. We can see if this worked by consulting the Rails routes table and seeing this entry in the resulting output:
$ bundle exec rails routes
      Prefix   Verb   URI Pattern                             Controller#Action
        root   GET    /                                       pages#hello
(If the output of the command is a bit messy for you, use the --expanded argument for a slightly nicer return).

With the above entry, we can be assured that Rails now knows what controller (Ruby class) and action (Ruby method) to consult if the user asks for the root of the site. However if we reload our Rails site, we can see that it isn't happy:

Rails error page showing lack of 'application#hello' action

The shrewd among you will know that this is because we haven't defined the pages#hello action - in fact the PagesController doesn't even exist yet!
We have instructed Rails what to do when receiving a request for the app root, but we haven't created the controller nor instructed it on what to do if this controller is asked to perform the hello action!

Let's fix that - luckily, the solution is simple. We'll create the PagesController in app/controllers/pages_controller.rb and add a #hello action to it with some instruction on what to do:

app/controllers/pages_controller.rb
class PagesController < ApplicationController ## defining classes = CamelCase def hello ## defining methods = snake_case render plain: "Hello, Bob!" end end
Note here that our PagesController is inheriting the functionality from the master controller, the ApplicationController.
The ApplicationController, which itself inherits all the magic Rails functionality from the ActionController and acts as an abstraction layer for it, is used as a sort of template for all the controllers that we create for our app in that any actions defined in it are inherited by all of our controllers. This isn't hugely necessary information at this early stage but, needless to say, I think saying hello to Bob is an action that we only need in one place!

Now if we navigate to our app’s root in the browser, we can see the fruit of our labour in glorious plain text! (Don't forget to start your server again if you stopped it before!)

Saying hi to Bob

Note the use of cases above in accordance with the convention that Ruby on Rails requires to work - because we are defining the application controller here (and it is a class) it is written in CamelCase, while the hello method inside it is defined in snake_case.

 
 

Comments

Popular posts from this blog

[ToDoList] Basic Pages

New Rails Apps with Docker Compose

[ToDoList] Building the App