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

Want to stay up-to-date with Ruby on Rails?

Join 74,071+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.