views:

1437

answers:

2

How do I change a rails app so that a controller foo appears as the application root?

In other words, right now all the urls look like host.com/foo/... and I'd like to get rid of the foo and have simply host.com/...

+7  A: 

In routes.rb, add:

map.root :controller => 'foo'

Full details in the API.

Abie
This worked to map / to /foo, but then I also wanted have requests to /foo send you back to / , so I changed places I had used foo_url to root_url in my code. Maybe there is a better way to do this using the RoR routing as well.
Bee
Don't forget to also remove the index.html page that comes with rails! (http://stackoverflow.com/questions/1205033/ruby-on-rails-map-root-doesnt-seem-to-be-working)
pkaeding
API link does not work
Joni
Edited the link to fix linkrot. Thank you, Joni, for the heads up.
Abie
+1  A: 

In your routes.rb you add a named route like so:

map.home '', :controller => 'foo', :action => 'index'

This will build a route for when the root of web application is requested, it will use the foo controller and call the index action. Make sure you have it at the bottom so it is given the lowest priority.

Dale Ragan