如何捕获routes.rb中的参数错误
在 RAils 应用程序的routes.rb 文件中,我们正在路由一些旧版 URL,如下所示:
get "downloads/item/:slug", :to => redirect {|params| "/downloads/#{params[:slug].gsub('.', '')}"}, :constraints => { :slug => /[^\/]+/ }
但是,问题是其中一些旧版 URL 具有不可编码的字符,例如省略号,这会破坏 gsub 命令出现错误:
NilClass# (ArgumentError)“UTF-8 中的无效字节序列”
我的问题是:如何捕获此异常,以及我的其他异常路线.rb 文件?我认为这与在文件末尾使用“包罗万象”路由到 404 不同。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果错误发生在块内,您可以使用
begin..rescue
阅读更多内容:http://ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html
If the error happens inside the block, you can use a
begin..rescue
Read more: http://ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html