How to avoid links to map.root getting shortened?
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
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
.
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
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.
精彩评论