How to add a Page to your Rails app

Blair Rorani

February 23, 2021

⚠️ This is how to add a page without any interaction with the model.

Create a route 🛤️

  1. Go to config > routes.rb file
  2. In the Rails.application.routes.draw method, enter a route using the syntax: [request type] "[URL]", to: "[controller]#[action]"

Create a controller 👨‍✈️

  1. Go to app > controllers folder
  2. Create a file named [route]_controller.rb, e.g. about_controller.rb
  3. Create a class named [Route]Controller and inherit from ApplicationController
  4. Define a method named [action from routes.rb], e.g. index

Create a view 🏞️

  1. Go to app > views folder
  2. Create a folder named [route]
  3. In that folder create a file named [action.html.erb]
  4. Add HTML (and ERB)

View new page 👀

  1. Go to http://localhost:3000/about
  2. You’ll see the new page

  3. Open Web Inspector
    👀 You’ll see the request from the browser and the response from the server for the GET request to the /about URL.

  4. View the page’s source code
    👀 You’ll see the content that Rails added code from app > views > layouts > application.html.erb and inserted the code from index.html.erb inside the <%= yield => ERB tag.
    Source code

application.html.erb with <%= yield => ERB tag

P.S. You might enjoy following me on Twitter.


Comments