加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何重定向到routes.rb中的404页面?

发布时间:2020-12-17 01:19:33 所属栏目:百科 来源:网络整理
导读:如何将不正确的URL重定向到routes.rb中的404页面? 现在我使用2个示例代码: # example 1match "/go/(*url)",to: redirect { |params,request| Addressable::URI.heuristic_parse(params[:url]).to_s },as: :redirect,format: false# example 2match "/go/(*
如何将不正确的URL重定向到routes.rb中的404页面?
现在我使用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-yedaa??a1fbbb /

谢谢

解决方法

如果你想要自定义错误页面,你最好看几周前写的 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

这显然是相对基础的,但希望它会给你一些关于你能做什么的更多想法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读