如何避免链接到map.root缩短?

时间:2010-03-30 23:31:46

标签: ruby-on-rails

我有一个报告控制器和各种报告:

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

我希望http://localhost默认为http://localhost/reports/main/this_month。使用我的routes.rb中的map.root很容易。

但是,当我执行此操作时,http://localhost/reports/main/this_month的所有链接现在都缩短为http://localhost。我希望链接保持完整

3 个答案:

答案 0 :(得分:2)

我认为在Rails 2中很有可能。生成的url字符串取决于你在视图中调用的url帮助器。

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"

现在,root_url将为http://locahost/。使用reports_url(:action => 'main', :timeframe => 'this_month')时,它将为http://localhost/reports/main/this_month。它们都呈现相同的动作。

听起来您已经设置了根目录,但只是不要使用root_url创建任何链接。

答案 1 :(得分:1)

一种选择是使用一个虚拟控制器来制作redirect_to

路线:

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

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

然后在欢迎控制器上:

class WelcomeController < Application: ApplicationController
  def index
    redirect_to :controller => "reports", :action => "main", :timeframe => "this_month"
  end
end

答案 2 :(得分:0)

据我所知,默认情况下这在Rails 2中是不可能的。但是,有一个名为Redirect Routing的插件允许它,您可以查看它。在Rails 3中,内置了此功能。您可以在The Lowdown on Routes in Rails 3在Engine Yard上阅读它。

相关问题