dragon dragon - 2 months ago 12
Ruby Question

Where should a root be located in your routes.rb file?

Probably something trivial but I was wondering where the root key word along with its controller action should be placed within the routes.rb file.

Rails.application.routes.draw do
get 'welcome/index'

root 'welcome#index'
end


I've taken a look at a couple of tutorials and most of them seem to have it at the bottom. Is there a reason for this?

Answer Source

According to the Rails routing guide

You should put the root route at the top of the file, because it is the most popular route and should be matched first.

and the partial reasoning is

Rails routes are matched in the order they are specified...

So, you want the most commonly used routes to be early on/high up in the file, so you don't have to go through and waste time checking a lot of uncommon or downright rare routes.