Adding a Root Route
In this demonstration, I will show how to make a page the “root” page—that is, the default page that is displayed if no resource path is added to the URL. We will continue to build upon the QuizMe project from the previous demos.
If you navigate to the QuizMe app’s root (http://localhost:3000), you’ll notice that it still has the old “Yay! You’re on Rails!”default page. We’d like this to be something more useful for our app, like the Welcome page.
Change what page the app’s root URL goes to by adding the following root route to the top of the block in the config/routes.rb
file:
root to: redirect('/welcome', status: 302)
More details about this code…▼
This statement means that whenever someone tries to go to http://localhost:3000
they are automatically redirected to the Welcome page URL via the route we previously made. If you didn't want to have a separate URL for the Welcome page, you could point root directly to the controller action with root to: 'static_pages#welcome'
and remove original welcome route. Then, in the link_to
statement, you would use the root_path
helper instead of welcome_path
.
Open the QuizMe app’s root URL, and confirm that the browser is redirected to the Welcome page.