2010-05-28 124 views
3

我有一個簡單的Rails應用程序作爲正在經歷過渡到新服務器的網站的初始頁面運行。由於這是一個建立的網站,我看到用戶請求打到Rails應用程序中不存在的頁面。將所有頁面查詢重定向到Rails中的主頁

如何將所有未知請求重定向到主頁而不是拋出路由錯誤?

回答

5

我剛剛使用過的路線通配符來實現這一目標:

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

請注意,這條路應該是在routes.rb中結束,讓所有其他路由才匹配。

+0

快速,完美,優雅地解決了我的問題。謝謝你的幫助! – 2010-05-28 17:05:13

0

您可以使用以下方法處理常見錯誤。將此代碼放置在您的應用程序中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 

修改它以滿足您的需求,您也可以重定向。

HTH

+0

如果只有幾個人去的地方,這可能會更有用,但不幸的是,我無法預測人們來自的所有鏈接。儘管感謝您的幫助。您可能會發現這裏發佈的基於路線的解決方案也很有趣! – 2010-05-28 17:19:26

相關問題