dragon
dragon

Reputation: 101

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?

Upvotes: 3

Views: 521

Answers (1)

Simple Lime
Simple Lime

Reputation: 11070

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.

Upvotes: 6

Related Questions