如何制作404页面ruby?
How to make 404 page ruby?
当我尝试访问 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
但是,当我尝试转到站点。ru/404 页面时,它显示我的工作 404 页面。
如果没有在routs注册,怎么给大家做一个404页面?
这是开发模式下的默认行为。在开发环境中,显示路由错误,以便开发人员可以注意到它们并修复它们。
如果你想看到 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 步:运行 下面的命令
rails generate controller errors not_found
第 2 步:打开并修改此控制器,如下所示
class ErrorsController < ApplicationController
def not_found
render(:status => 404)
end
end
第三步:更新路由如下
match "/404", to: "errors#not_found", via: :all
第四步:更新配置文件application.rb
config.exceptions_app = self.routes
第 5 步:从 public 文件夹中删除默认模板,例如 404.html
然后重新启动您的服务器然后测试 localhost:3000/404
它会自动在 production
服务器上运行
详情可以看this blog post
当我尝试访问 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
但是,当我尝试转到站点。ru/404 页面时,它显示我的工作 404 页面。
如果没有在routs注册,怎么给大家做一个404页面?
这是开发模式下的默认行为。在开发环境中,显示路由错误,以便开发人员可以注意到它们并修复它们。 如果你想看到 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 步:运行 下面的命令
rails generate controller errors not_found
第 2 步:打开并修改此控制器,如下所示
class ErrorsController < ApplicationController
def not_found
render(:status => 404)
end
end
第三步:更新路由如下
match "/404", to: "errors#not_found", via: :all
第四步:更新配置文件application.rb
config.exceptions_app = self.routes
第 5 步:从 public 文件夹中删除默认模板,例如 404.html
然后重新启动您的服务器然后测试 localhost:3000/404
它会自动在 production
服务器上运行
详情可以看this blog post