我正在使用Rails應用程序。在我的應用程序中,如果我在地址欄中手動輸入自定義路由/作爲config/routes.rb中不存在的URL,它將顯示下面給出的錯誤消息。應該重定向到自定義路由/頁面上的常見顯示/頁面在Rails中發現錯誤
路由錯誤
沒有路由匹配「/ clientImage/blablahblah」
我想這被重定向到一個合適的顯示器用戶不管是有意/無意給所有的錯誤路線。任何幫助將不勝感激。
我正在使用Rails應用程序。在我的應用程序中,如果我在地址欄中手動輸入自定義路由/作爲config/routes.rb中不存在的URL,它將顯示下面給出的錯誤消息。應該重定向到自定義路由/頁面上的常見顯示/頁面在Rails中發現錯誤
路由錯誤
沒有路由匹配「/ clientImage/blablahblah」
我想這被重定向到一個合適的顯示器用戶不管是有意/無意給所有的錯誤路線。任何幫助將不勝感激。
當有人進入網址不受支持Rails會提高的ActionController :: RoutingError。你可以拯救這個錯誤,並呈現404 Not Found html。
爲此,Rails提供了一些稱爲rescue_from的特殊功能。
class ApplicationController < ActionController::Base
rescue_from ActionController::RoutingError, :with => :render_not_found
rescue_from StandardError, :with => :render_server_error
protected
def render_not_found
render "shared/404", :status => 404
end
def render_server_error
render "shared/500", :status => 500
end
end
把你404.html,500.html在app /視圖/共享
Yourapp::Application.routes.draw do
#Last route in routes.rb
match '*a', :to => 'errors#routing'
end
「a」實際上是Rails 3路徑全局技術中的一個參數。例如,如果你的網址是/ this-url-does-not-exist,那麼params [:a]等於「/ this-url-does-exist-exist」。所以,儘可能創造性地處理那個流氓路線。
在應用程序/控制器/ errors_controller.rb
class ErrorsController < ApplicationController
def routing
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end
這意味着我需要爲所有URL(對於每個控制器)具有「匹配」語句。對?這將是一項乏味的任務。 –
您在開發環境中可能工作。在製作中,您只需在公共目錄中放置一個404.html頁面來自定義顯示 – sailor
是的,我正在開發env。感謝您的信息。 –