Rails生成404,500个错误页面,其布局与其余网页相同

时间:2013-03-06 13:17:06

标签: ruby-on-rails

我如何在生产模式下渲染我的错误页面,以便它们与其他页面的布局相同?例如,不是404作为标准

<h1>The page you were looking for doesn't exist.</h1>
    <p>You may have mistyped the address or the page may have moved.</p>

没有任何布局,但我的布局中的这条消息(称为application.html.haml)?

这是真的吗?我需要写什么和哪里?我google'd但是对于自己的布局没有找到好的....

我使用rails 3.2.8,ruby 1.9.3

2 个答案:

答案 0 :(得分:4)

一个解决方案就是:

# In config/application.rb
config.exceptions_app = self.routes

# In routes
match "/404", to: "errors#not_found"
match "/500", to: "errors#server_error"

# app/controllers/errors_controller.rb

class ErrorsController < ApplicationController
  # Inherits layout from ApplicationController 

  def not_found
  end

  def server_error
  end
end

# app/views/errors/not_found.haml
%h1 Didn't find nothing!

# app/views/errors/server_error.haml
%h1 FUBAR!

答案 1 :(得分:0)

我使用这种方法,你应该找到相同的Rails 3

http://henrik.nyh.se/2008/07/rails-404/