2011-04-22 149 views
1

我想設置我的404在軌...Ruby on Rails的3 - 404路由

我按照these說明

其似乎工作,如果不這樣做:

www.myapp.com/no_controller/

,但如果我這樣做:

www.myapp.com/existing_controller/no_action

我得到活動記錄,找不到記錄......等等......

我想這也是路由到404頁面......這可能嗎?

+0

404錯誤是某種自動路由。它會自動顯示「public/404.html」頁面的4​​04錯誤 – fl00r 2011-04-22 20:37:16

回答

9

當你去

www.myapp.com/existing_controller/no_action 

你實際調用與no_action作爲idexisting_controllershow行動。在開發模式下,你會得到RecordNotFound錯誤。在製作過程中,您將獲得一個404頁面。 (!順便說一句,我不建議,因爲它是故意幫你調試)

如果你想定製的開發模式和根,以404頁的這種行爲,你可以rescue_from此錯誤:

class ApplicationController < ActionController::Base 

    rescue_from ActiveRecord::RecordNotFound do 
    render_404 
    end 

    def render_404 
    respond_to do |type| 
     type.html { render :template => "shared/error_404/message", :layout => "application", :status => "404 Not Found" } 
     type.all { render :nothing => true, :status => "404 Not Found" } 
    end 
    end 
end 

超出範圍。設計404頁面時,此示例中的一種技術很有用:與標準public/404.html不同,您可以使用此方法的應用程序佈局。

+0

真棒,感謝Voldy! – Paul 2011-04-22 21:38:37