5.4. Setting the
Default Root
Typing http://127.0.0.1:3000/photos/list
or http://localhost:3000/photos/list
is getting tedious. It would be easier to use http://127.0.0.1:3000/ and be directed
to whatever page you want to designate as the starting page. Rails
handles all of the URL mapping itself, so you can easily shorten
redundant URLs. config/routes.rb
controls the routing for the
application, so you need to edit this file and find this of
comments:
# You can have the root of your site routed by hooking up ''
# -- just remember to delete public/index.html.
# map.connect '', :controller => "welcome"
Now, uncomment the last line and change it
to:
map.connect '', :controller => "photos", :action => "list"
With this new routing rule, any time Rails sees
an empty URL (represented by the '' parameter), it should
invoke the list action in the photos controller.
Before this change will work, you need to delete the public/index.html file. If you don't, the web
server will serve up index.html
instead of list.rhtml whenever you
browse to http://127.0.0.1:3000/. Because the
index.html is static, Rails will
never get called.
Now try browsing to http://127.0.0.1:3000/;
you should see a nice new photos/list page, complete with thumbnails and
navigation bar. |