【发布时间】:2011-01-15 08:17:32
【问题描述】:
我需要在我的 rails 应用程序中实现一个自定义错误页面,以允许我使用 erb。
我一直在关注本教程 (http://blog.tommilewski.net/2009/05/custom-error-pages-in-rails/),但我无法让它在本地(或远程)工作。我正在运行 Rails 2.3.5
这是该方法的要点。
1) 在“application_controller”中,我覆盖了“render_optional_error_file(status_code)”方法,并将可见性设置为“受保护”,如下所示。
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”
我似乎无法让它工作。我一直试图通过导航到http://localhost:3000/foobar 来触发 404 页面——但是,我没有得到新的404.html.erb,而是似乎得到了标准的 apache 500 错误。当我同时尝试mongrel_rails start 和mongrel_rails start -e production 时会发生这种情况。
【问题讨论】:
标签: ruby-on-rails error-handling routing