【问题标题】:Custom error pages with ThoughtBot High Voltage使用 ThoughtBot 高压的自定义错误页面
【发布时间】:2013-03-14 23:22:38
【问题描述】:

我在 rails 3.2.2 应用程序上使用 1.2.2 高压,并且似乎无法让我的应用程序在找不到页面时触发自定义 404 - 我只是收到“路由错误” , 没有这样的页面:“错误页面。

Routes.rb:

...

match '/signup',  to: 'users#new'

root :to => 'high_voltage/pages#show', :id => 'home'

match '/404', :to => 'errors#not_found'
match '/422', :to => 'errors#server_error'
match '/500', :to => 'errors#server_error'

match '/:id' => 'high_voltage/pages#show', :as => :static, :via => :get

我还在 application.rb 中设置了以下内容(我相信这是 Rails 3.2 引发路由错误的方式):

config.exceptions_app = self.routes

但我仍然得到默认的路由错误页面。

我也尝试在我的应用控制器中使用它:

unless config.consider_all_requests_local
        rescue_from Exception, :with => :render_500
        rescue_from ActiveRecord::RecordNotFound, :with => :render_404
        rescue_from ActionController::RoutingError, :with => :render_404
        rescue_from ActionController::UnknownController, :with => :render_404
        rescue_from ActionController::UnknownAction, :with => :render_404
    end

    protected

    def render_500 exception
      logger.error exception.inspect
      render :template => "/errors/server_error.html.haml", :status => 500, :layout => 'application'
    end

    def render_404 exception
      logger.error exception.inspect
      render :template => "/errors/not_found.html.haml", :status => 404, :layout => 'application'
    end

但还是不行。

我被难住了!有人可以帮忙吗?

【问题讨论】:

  • 你删除/public中的404.html和500.html文件了吗?
  • 在你建议之前我没有,但它仍然是一样的......

标签: ruby-on-rails ruby high-voltage


【解决方案1】:

通过以下修改,我可以获得一个使用高压的自定义 404 页面:

注意:如果您在本地进行测试,则需要运行 RAILS_ENV=production rails s,因为开发模式会显示不同的 404 错误页面。

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

# config/routes.rb
get '/404', to: 'errors#not_found'

然后我们只需要创建一个ErrorsController 和一个errors/not_found.html.erb 的视图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 2015-06-22
    相关资源
    最近更新 更多