代码之家  ›  专栏  ›  技术社区  ›  Guy C

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

  •  1
  • Guy C  · 技术社区  · 15 年前

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

    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 . 在my routes.rb中使用map.root很容易。

    但是,当我这样做的任何链接 http://localhost/reports/main/this_month 现在缩短为 http://localhost . 我希望链接保持完整

    3 回复  |  直到 15 年前
        1
  •  2
  •   Harish Shetty    15 年前

    我认为这在Rails2中是非常可能的。生成的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 . 它们都呈现相同的动作。

    听起来好像你已经设置了根目录,但是不要创建任何链接 根url

        2
  •  1
  •   kikito    15 年前

    一种选择是使用一个虚拟控制器,使 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
    
        3
  •  0
  •   Jimmy Nitzan Tomer    15 年前

    Redirect Routing 然而,这将允许你去调查。在Rails3中,这个功能是内置的。你可以在报纸上看到 The Lowdown on Routes in Rails 3 在发动机厂。