Rails Root Route

How to Add a root Route in Rails

2 min read
💡
This post is part of my handbook on the Rails Router.
The Rails Router
Everything you need to know about the incredible routing system in Rails.

Your application's home page is one of the most important and highly trafficked page. This is the page many visitors will encounter the first time they visit your application.

The root helper method configures the home page of your Rails app.

Rails.application.routes.draw do
  # Defines the root path route ("/")
  root "application#home"
end

The root indicates the application URL '/', without any path, such as /users.

The router parses the routes top-down and stops search as soon as it finds a matching route. Hence, it's recommended that you should put the root route at the top of the routes file. It is the most popular route which should be matched first.

Since most visitors will be visiting the application by making a GET request to your application URL, the root route only handles and routes HTTP GET requests.

You can also use the root helper inside a namespaces and scope.

namespace :admin do
  root to: "admin#index"
end

root to: "home#index"

If you don't provide the root route, Rails routes the request to the home page to an internal controller Rails::WelcomeController which renders the welcome page.

rails welcome page
rails welcome page

That's a wrap. I hope you found this article helpful and you learned something new.

As always, if you have any questions or feedback, didn't understand something, or found a mistake, please leave a comment below or send me an email. I reply to all emails I get from developers, and I look forward to hearing from you.

If you'd like to receive future articles directly in your email, please subscribe to my blog. Your email is respected, never shared, rented, sold or spammed. If you're already a subscriber, thank you.