Setting a Default Page
Before moving on to more âseriousâ concerns about developing applications, thereâs one question that web developers always seem to ask about 15 minutes into their first Rails experience:
How do I set a default page for the application?
The Rails welcome page, shown in Figure 1-2, is just plain ugly. There are two ways to change that:
Edit the public/index.html file and put in something more to your liking
Delete the public/index.html file and tweak the config/routes.rb file
The first one is pretty easy, but it doesnât integrate very tightly with your Rails application. The second approach lets you pick a controller that will run if the Rails application is run without specifying a controllerâthat is, in the test environment, by directly visiting http://localhost:3000/.
To make this work, youâll need to enter an extra line in the config/routes.rb file. Near the bottom of that, youâll see:
# You can have the root of your site routed with map.root -- # just remember to delete public/index.html. # map.root :controller => "welcome"
Change the last line of that to:
map.root :controller => "hello", :action => "index"
Save the file, make sure youâve deleted or renamed the public/index.html file, and restart your server. You should see something like Figure 3-6.
Figure 3-6. Accessing a controller by default, when the URL doesnât specify one
Donât worry if ...
Get Learning Rails: Live Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.