Rails中的自定义错误页面? [英] Custom Error Pages in Rails?

查看:79
本文介绍了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

这是这个方法的要点。

Here's the gist of the approach.

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 ,我创建了一个新的布局错误.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开始 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

这会使错误布局错误/ error_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






/ strong>


Edit

更多的解释为什么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.

Rails票4444


如果您的应用程序依赖于扩展您的应用程序与他们的
自己的路线,事情会破坏,因为这些路线永远不会得到
被激发!

"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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆