render default template when requested template is missing in Rails

后端 未结 4 984
半阙折子戏
半阙折子戏 2021-01-19 04:19

For a plugin I want to hack the following feature into Rails:

When a (partial) template does not exist (regardless of the format) I want to render a default template

4条回答
  •  再見小時候
    2021-01-19 04:54

    I found a patch that is relatively clean, it only patches the lookup of the template which is exactly what was required in the question.

    
    module ActionView
      class PathSet
    
        def find_template_with_exception_handling(original_template_path, format = nil, html_fallback = true)
          begin
            find_template_without_exception_handling(original_template_path, format, html_fallback)
          rescue ActionView::MissingTemplate => e
            # Do something with original_template_path, format, html_fallback
            raise e
          end
        end
        alias_method_chain :find_template, :exception_handling
    
      end
    end
    

提交回复
热议问题