自定义错误页面 - Ruby on Rails

发布于 2024-10-09 05:32:45 字数 1336 浏览 1 评论 0原文

我正在尝试在我的网站中设置自定义错误页面。我遵循PerfectLine 博客

它适用于控制器存在但 id 不存在的情况。例如,我有一个博客控制器,但 id 4 不存在。它显示了自定义错误页面

,但在控制器本身不存在的情况下,它不存在。例如,如果我输入一些带有数字 id 的随机控制器,我在应用程序控制器中设置的重新路由自定义错误页面的方法不会捕获它。在这种情况下,我在终端中收到

ActionController::RoutingError (No Route matches "/randomcontrollername"):

以及 Rails 附带的默认错误页面。

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  unless Rails.application.config.consider_all_requests_local
    rescue_from Exception,                            :with => :render_error
    rescue_from ActiveRecord::RecordNotFound,         :with => :render_not_found
    rescue_from ActionController::RoutingError,       :with => :render_not_found
    rescue_from ActionController::UnknownController,  :with => :render_not_found
    rescue_from ActionController::UnknownAction,      :with => :render_not_found
  end

  private
  def render_not_found(exception)
     render :template => "/error/404.html.erb", :status => 404
  end

  def render_error(exception)
    render :template => "/error/500.html.erb", :status => 500 
  end

end

你能帮我一下吗?谢谢。

I am trying to setup a custom error page in my website. I am following the guidelines atPerfectLine Blog.

It works in the case where the controller exists, but the id does not exist. For example, I have a blog controller and id 4 does not exist. It shows the custom error page

But it does not exist in the case, where controller itself does not exist. For example, if I type some random controller with a numeric id does not gets caught by the methods I have setup in the application controller to re-route the custom error pages. In this case, I get an

ActionController::RoutingError (No route matches "/randomcontrollername"):

in the terminal and the default error page that comes with rails.

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  unless Rails.application.config.consider_all_requests_local
    rescue_from Exception,                            :with => :render_error
    rescue_from ActiveRecord::RecordNotFound,         :with => :render_not_found
    rescue_from ActionController::RoutingError,       :with => :render_not_found
    rescue_from ActionController::UnknownController,  :with => :render_not_found
    rescue_from ActionController::UnknownAction,      :with => :render_not_found
  end

  private
  def render_not_found(exception)
     render :template => "/error/404.html.erb", :status => 404
  end

  def render_error(exception)
    render :template => "/error/500.html.erb", :status => 500 
  end

end

Could you please help me. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城泪 2024-10-16 05:32:45

您可以通过 Rails 中的路线通配符来做到这一点,它可以让您使用通配符将任何操作与路线的任何部分相匹配。

要捕获所有剩余路由,只需在 config/routes.rb 中定义一个低优先级路由映射作为最后一个路由:

在 Rails 3 中:
匹配“*路径”=> 'error#handle404'

在 Rails 2 中:
map.connect "*path", :controller =>; '错误', :action => 'handle404'

params[:path] 将包含匹配的部分。

You can do that with route globbing in rails, It lets you match any action to any part of a route using wildcards.

To catch all remaining routes, just define a low priority route mapping as the last route in config/routes.rb:

In Rails 3:
match "*path" => 'error#handle404'

In Rails 2:
map.connect "*path", :controller => 'error', :action => 'handle404'

params[:path] will contain the matching part.

书信已泛黄 2024-10-16 05:32:45

如果您不需要动态错误页面,只需编辑 public/404.htmlpublic/505.html 即可。如果您这样做,请参阅 Reza.mp 的回答。

If you don't need dynamic error pages, just edit public/404.html and public/505.html. If you do, see Reza.mp's answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文