代码之家  ›  专栏  ›  技术社区  ›  Dean Putney

将所有页面查询重定向到Rails中的主页

  •  3
  • Dean Putney  · 技术社区  · 14 年前

    我有一个简单的Rails应用程序作为网站的初始页面运行,它正在向新服务器过渡。由于这是一个已建立的网站,我看到用户请求正在访问Rails应用程序中不存在的页面。

    如何将所有未知请求重定向到主页而不是引发路由错误?

    2 回复  |  直到 14 年前
        1
  •  5
  •   Alex Korban    14 年前

    我只是使用了路线全局搜索来实现这一点:

    map.connect "/*other", :controller => "pages", :action => "index"
    

    请注意,此路由应位于routes.rb的末尾,以便所有其他路由在其之前匹配。

        2
  •  0
  •   Rishav Rastogi    14 年前

    您可以使用以下方法来处理常见位置的错误。将此代码放入ApplicationController

      def rescue_404
        @message = "Page not Found"
        render :template => "shared/error", :layout => "standard", :status => "404"
      end
    
      def rescue_action_in_public(exception)
        case exception
          when CustomNotFoundError, ::ActionController::UnknownAction then
            #render_with_layout "shared/error404", 404, "standard"
            render :template => "shared/error404", :layout => "standard", :status => "404"
          else
            @message = exception
            render :template => "shared/error", :layout => "standard", :status => "500"
        end
      end
    
    

    修改它以满足您的需要,您也可以有重定向。

    高温高压