Render default template on MissingTemplate Errors

蹲街弑〆低调 提交于 2019-12-11 10:07:35

问题


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

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. How to catch the exception?
  2. How to render a view afterwards?

Rails version - 3.0.19

Thanks in advance!


回答1:


Possible duplicate of: render default template when requested template is missing in Rails

Which says:

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


来源:https://stackoverflow.com/questions/14566477/render-default-template-on-missingtemplate-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!