如何将不正确的URL重定向到routes.rb中的404页面?
现在我使用2个示例代码:
现在我使用2个示例代码:
# example 1 match "/go/(*url)",to: redirect { |params,request| Addressable::URI.heuristic_parse(params[:url]).to_s },as: :redirect,format: false # example 2 match "/go/(*url)",request| Addressable::URI.heuristic_parse(URI.encode(params[:url])).to_s },format: false
但是当我尝试在’url’参数中使用俄语单词时,在第一个例子中我得到500页(坏URI),在第二个 – 我得到重定向到stage.example.xn – org-yedaaa1fbbb /
谢谢
解决方法
如果你想要自定义错误页面,你最好看几周前写的
this answer
您需要几个重要元素来创建自定义错误路径:
– >在application.rb中添加自定义错误处理程序:
#config/application.rb config.exceptions_app = self.routes
– >在routes.rb文件中创建/ 404路由:
config/routes.rb if Rails.env.production? get '404',:to => 'application#page_not_found' end
– >向应用程序控制器添加操作以处理这些路由
#app/controllers/application_controller.rb def page_not_found respond_to do |format| format.html { render template: 'errors/not_found_error',layout: 'layouts/application',status: 404 } format.all { render nothing: true,status: 404 } end end
这显然是相对基础的,但希望它会给你一些关于你能做什么的更多想法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。