Dynamic Front Page

You’ve created your amazing Ruby on Rails application. And now, you want to get rid of that standard “Welcome to RoR!” front page. How do you do it?

Look in /public. There’s a file called index.html. Delete it, and replace it with your own static front page.

But what if you want a dynamic front page? One that does things like list links to different objects?

Well, you can always create a new model called FrontPage, and map the index page to one of the views. This way, you can include code in your front page.

Here’s how to do that:

New in Rails 2.0: You can specify the map.root directive, with a controller and an optional action, like so:

map.root :controller => :something, :action => :some_action

… and Rails will map the root URL to that action! Or, for Rails 1.2 users:

map.connect '', :controller => :something, :action => :some_action

You’ll also need to:

  • Delete /public/index.html
  • Modify the controller so that it has the action you specified
  • Create a new file in your view for your action

(You can also create a new controller just for the front page, if you like, and use that.)

And that’s it! Anytime you go to http://www.yourdomain.com/, it will route the user to the specified view of the specified controller. If you want to add code (eg. list some objects), just throw the code into the front page controller file.

About Ashiq Alibhai

Ashiq Alibhai, PMP, has been a Rails aficionado since 2007, and developed web applications since early 2003, where he learned PHP in one summer. As the driving-force behind RailsRocket and the Launchpad project, he seeks to share the ease of development with Rails far and wide.
This entry was posted in Development and tagged , , . Bookmark the permalink.

Comments are closed.