在 MissingTemplate 错误时呈现默认模板 [英] Render default template on MissingTemplate Errors

查看:39
本文介绍了在 MissingTemplate 错误时呈现默认模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Rails 中捕获 ActionView::MissingTemplate 错误?

How can I catch the ActionView::MissingTemplate errors in Rails?

例如,我的 PeopleController 有一个 Index 方法,但它不需要模板.当有人浏览到 root_url/people 时,他们会得到默认的静态错误模板.

For example, my PeopleController has an Index method, but it doesn't need a template. When someone browses to root_url/people they get the default static error template.

这不是唯一有问题的控制器;我希望所有丢失的模板错误都将用户重定向到我的自定义视图.

And that's not the only controller with the issue; I want all missing template errors to redirect the user to my custom view.

  1. 如何捕捉异常?
  2. 之后如何渲染视图?

Rails 版本 - 3.0.19

Rails version - 3.0.19

提前致谢!

推荐答案

可能重复:当 Rails 中缺少请求的模板时渲染默认模板

说的是:

在 ApplicationController 中使用rescue_from":

Use 'rescue_from' in ApplicationController:

class ApplicationController < ActionController::Base
  rescue_from ActionView::MissingTemplate do |exception|
    # use exception.path to extract the path information
    # This does not work for partials
  end
end

这篇关于在 MissingTemplate 错误时呈现默认模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆