Rails 中的自定义错误页面? [英] Custom Error Pages in Rails?
问题描述
我需要在我的 rails 应用程序中实现一个自定义错误页面,以允许我使用 erb.
I need to implement a custom error page in my rails application that allows me to use erb.
我一直在关注本教程(http://blog.tommilewski.net/2009/05/custom-error-pages-in-rails/) 并且我无法让它在本地(或远程)工作.我正在运行 Rails 2.3.5
I've been following this tutorial (http://blog.tommilewski.net/2009/05/custom-error-pages-in-rails/) and I cannot get it to work locally (or remotely). I am running Rails 2.3.5
这是该方法的要点.
1) 在application_controller"中,我覆盖了render_optional_error_file(status_code)"方法,并将可见性设置为protected",就像这样.
1) in the 'application_controller', I over ride the "render_optional_error_file(status_code)" method, and set the visibility to "protected", like this.
protected
def render_optional_error_file(status_code)
known_codes = ["404", "422", "500"]
status = interpret_status(status_code)
if known_codes.include?(status_code)
render :template => "/errors/#{status[0,3]}.html.erb", :status => status, :layout => 'errors.html.erb'
else
render :template => "/errors/unknown.html.erb", :status => status, :layout => 'errors.html.erb'
end
end
def local_request?
true
end
我还在视图中创建了一个名为 errors
的文件夹,并创建了以下视图:404.html.erb
、422.html.erb
, 500.html.erb
,unknown.html.erb
和我创建了一个新的布局errors.html.erb"
I also created a folder within views called errors
and created the following views: 404.html.erb
, 422.html.erb
, 500.html.erb
,unknown.html.erb
and I created a new layout "errors.html.erb"
我似乎无法让它工作.我一直试图通过导航到 http://localhost:3000/foobar
来触发 404 页面——但是,而不是获取新的 404.html.erb
,我似乎遇到了标准的 apache 500 错误.当我同时尝试 mongrel_rails start
和 mongrel_rails start -e production
时会发生这种情况.
I can't seem to get it to work. I've been trying to trigger the 404 page by navigating to http://localhost:3000/foobar
-- but, instead of getting the new 404.html.erb
, I seem to be getting the standard apache 500 error. This happens when I try both mongrel_rails start
and mongrel_rails start -e production
.
推荐答案
我建议使用异常来呈现此类错误页面,以便您可以使用继承来对错误消息进行分组...
I would suggest using exceptions to render such error pages, so you can use inheritance to group your error messages...
首先声明一些(我通常在application_controller.rb中做)
First, declare some (I usually do it in application_controller.rb)
class Error404 < StandardError; end
class PostNotFound < Error404; end
然后在ApplicationController中添加代码来处理它们
Then add code to ApplicationController to handle them
class ApplicationController < ActionController::Base
# ActionController::RoutingError works in Rails 2.x only.
# rescue_from ActionController::RoutingError, :with => :render_404
rescue_from Error404, :with => :render_404
rescue_from PostNotFound, :with => :render_post_not_found
def render_404
respond_to do |type|
type.html { render :template => "errors/error_404", :status => 404, :layout => 'error' }
type.all { render :nothing => true, :status => 404 }
end
true
end
def render_post_not_found
respond_to do |type|
type.html { render :template => "errors/shop_not_found", :status => 404, :layout => 'error' }
type.all { render :nothing => true, :status => 404 }
end
true
end
end
这会使用错误布局呈现错误/错误_404.应该让你开始:)
This renders errors/error_404 with the errors layout. Should get you started :)
在你的 target_controller 中:
And in your target_controller:
raise PostNotFound unless @post
<小时>
编辑
关于为什么 ActionController::RoutingError 不适用于 rails 3 的详细解释:Rails 3.0 异常处理.
for a longer explanation on why ActionController::RoutingError doesn't work for rails 3: Rails 3.0 Exception Handling.
如果您的应用程序依赖于使用其扩展应用程序的引擎自己的路线,事情会破裂,因为这些路线永远不会得到开除!"
"If your application relies on engines that extend your app with their own routes, things will break because those routes will never get fired!"
这篇关于Rails 中的自定义错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!