如何制作404页红宝石?

时间:2018-12-30 19:25:52

标签: ruby-on-rails ruby routing http-status-code-404

当我尝试转到site.ru/gdfgsdf页面时,它说:

Routing Error
No route matches [GET] "/gfdg"
Rails.root: /site.ru

Application Trace | Framework Trace | Full Trace
Routes
Routes match in priority from top to bottom

但是,当我尝试访问site.ru/404页面时,它显示了我的工作404页面。

如果未在溃败中注册,如何为所有人制作404页面?

2 个答案:

答案 0 :(得分:3)

这是开发人员模式下的默认行为。在开发环境中,将显示路由错误,以便开发人员可以注意到并修复它们。 如果要查看404页面,请以生产模式启动服务器并进行检查。

$ rails s -e production # script/rails s -e production

或者,如果您不想始终以生产模式运行服务器,请尝试在ApplicationController中执行此操作:

rescue_from ActiveRecord::RecordNotFound, :with => :rescue_404
rescue_from ActionController::RoutingError, :with => :rescue_404

def rescue_404
  #custom behavior
end

答案 1 :(得分:0)

您可以很容易地做到这一点,例如按照步骤操作

步骤1:运行以下命令

rails generate controller errors not_found

第2步:打开并修改此控制器,如下所示

class ErrorsController < ApplicationController
    def not_found
        render(:status => 404)
    end
end

第3步:更新以下路线

match "/404", to: "errors#not_found", via: :all

步骤4:更新配置文件application.rb

config.exceptions_app = self.routes

第5步:从公用文件夹(如404.html)中删除默认模板

然后重新启动服务器,然后测试localhost:3000/404,它将自动在production服务器上工作

有关详细信息,您可以看到this blog post

相关问题