# Missing Template Should be 404 Error

**URL:** <https://www.ruby-forum.com/t/missing-template-should-be-404-error/83204>\
**Category:** Rails\
**Created:** [January 24, 2007, 10:08am UTC](https://www.ruby-forum.com/t/missing-template-should-be-404-error/83204 "2007-01-24T10:08:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Longwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aaron\_Longwell](https://www.ruby-forum.com/u/Aaron_Longwell)\
**Post date:** [January 24, 2007, 10:08am UTC](https://www.ruby-forum.com/t/missing-template-should-be-404-error/83204/1 "2007-01-24T10:08:56Z")

</div>

I have a site I’m building that has the template dynamically chosen  
based  
upon some parameters of the request. There are cases where a nonexistent  
template will be sent to the render method. For purposes of my  
application,  
this is not a serious, fatal error, but a simple HTTP 404 error code.  
Unfortunately, Rails disagrees, and it’s sending a 500 error message in  
production.

How can I override this behavior?

What is the justification for template missing to be treated as a 500  
error  
instead of 404 error?

---

<div class="post-metadata">

**Author:** ![Aaron\_Longwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aaron\_Longwell](https://www.ruby-forum.com/u/Aaron_Longwell)\
**Post date:** [January 24, 2007, 10:18am UTC](https://www.ruby-forum.com/t/missing-template-should-be-404-error/83204/2 "2007-01-24T10:18:02Z")

</div>

[http://6brand.com/rails-custom-404-pages](http://6brand.com/rails-custom-404-pages)

Aaron Longwell  
escribió:

> I have a site I’m building that has the template dynamically chosen based

---

<div class="post-metadata">

**Author:** ![Aaron\_Longwell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aaron\_Longwell](https://www.ruby-forum.com/u/Aaron_Longwell)\
**Post date:** [January 24, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/missing-template-should-be-404-error/83204/3 "2007-01-24T18:03:50Z")

</div>

That site is similar to a lot of other stuff I’ve seen. It appears that  
catching missing method errors is a much more common problem than  
catching  
missing _template_ errors.

I can’t use method\_missing, because my error happens after the call to  
render (after the correct action method has begun executing)… and I  
don’t  
believe template\_missing exists… but I have found a (the?) solution.

As it turns out, rescue\_action\_in\_public can catch the MissingTemplate  
error, but I was not detecting the error properly in my previous  
attempts. I  
was failing to use global scope when specifying Missing Template. Here’s  
what works:

if exception.kind\_of? ::ActionController::MissingTemplate  
render :status =\> 404, :text =\> (IO.read(File.join(RAILS\_ROOT,  
‘public’, ’  
404.html’)))  
end

That code is effectively converting 500 errors from MissingTemplate into  
404  
errors just like other rails features. Excellent.

Notice the “::” before ActionController. Failing to include that results  
in  
the exception handler throwing an error of its own… which I didn’t  
notice  
before.
