我正在使用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。感谢您的信息。 –