Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 224
dragon Avatar asked Nov 20 '25 01:11

dragon


1 Answers

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.

like image 105
Simple Lime Avatar answered Nov 22 '25 13:11

Simple Lime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!