views:

40

answers:

3

I have a Reports controller and various reports:

http://localhost/reports/main/this_month
http://localhost/reports/main/last_month
http://localhost/reports/main/this_year

I wanted http://localhost to default to http://localhost/reports/main/this_month. That is easy enough using map.root in my routes.rb.

However when I do this any links to http://localhost/reports/main/this_month are now shortened to just http://localhost. I want the links to stay full

A: 

As far as I know this is not possible in Rails 2 by default. There is a plugin called Redirect Routing that will allow it, however, which you could look into. In Rails 3, this functionality is built in. You can read about it in The Lowdown on Routes in Rails 3 over at Engine Yard.

Jimmy Cuadra
If you put the monthly_reporting route above the root route it will match that first.
Ryan Bigg
+2  A: 

I think it is very possible in Rails 2. The url string that is generated depends on which url helper you call in your view.

map.reports '/reports/:action/:timeframe', :controller => :reports
# todo pretty this up with some more named routes for reports
map.root :controller => "reports", :action => "main", :timeframe => "this_month"

Now, root_url will be http://locahost/. When you use reports_url(:action => 'main', :timeframe => 'this_month'), it will be http://localhost/reports/main/this_month. They both render the same action.

It sounds like you have set up the root, but just don't create any links with root_url.

Jonathan Julian
Yes, map.reports and using reports_url does the trick. I don't really understand why reports_url doesn't shorten but the following does: <%= link_to_unless_current 'This Month', :controller => 'reports', :action => 'main', :timeframe => 'this_month' %>
Guy C
More problems: using a named route for reports now means that if I use <%= link_to_unless_current 'This Month', reports_url(:action => 'main', :timeframe => 'this_month') %> to disable the current page in the menu, this code does not treat http://locahost/ as current.
Guy C
Favor the `_path` named routes over their `_url` counterparts on site pages.`link_to_unless_current` uses the current url to see if there is a match - you may want to override it in `application_helpers.rb` to handle your special case.
Jonathan Julian
+1  A: 

One option is using a dummy controller that makes a redirect_to.

Routes:

map.reports '/reports/:action/:timeframe', :controller => :reports

# this triggers the action 'index' on 'welcome'
map.root :controller => "welcome"

And then on the Welcome controller:

class WelcomeController < Application: ApplicationController
  def index
    redirect_to :controller => "reports", :action => "main", :timeframe => "this_month"
  end
end
egarcia
That works, but it's a redirect. So the user no longer sees `/` in the url bar. And it's a 2nd request - totally unnecessary.
Jonathan Julian
I see. I thought he wanted "not to see" the / .
egarcia
As Jonathan points out, I don't mind seeing / in the browser's url bar, it is the links in my app where I don't want to see /
Guy C
However I now getting problems with link_to_unless_current so I might actually just go for a redirect for now.
Guy C
Well, I'm glad I could help. As a side note, I've investigated and redirects don't generate a second request on the browser, so the performance penalty shouldn't be that bad.
egarcia