How to add a Root Route to your Rails app

Blair Rorani

February 23, 2021

You need to replace the default, “Yay! You’re on Rails” landing page with a real index page for your app.

Create a route 🛤️

  1. Go to config > routes.rb file
  2. In the Rails.application.routes.draw method, enter the route: get root, to: "main#index"

routes.rb

Create a controller 👨‍✈️

  1. Go to app > controllers folder
  2. Create a file named main_controller.rb
  3. Create a class named MainController and inherit from ApplicationController
  4. Define a method named index.

main_controller.rb

Create a view 🏞️

  1. Go to app > views folder
  2. Create a folder named main
  3. In that folder create a file named index.html.erb
  4. Add HTML

app > views > main > index.html.erb

View new page 👀

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

localhost:3000/

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


Comments