# How do I manually throw a 404?

**URL:** <https://www.ruby-forum.com/t/how-do-i-manually-throw-a-404/58527>\
**Category:** Rails\
**Created:** [April 22, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/how-do-i-manually-throw-a-404/58527 "2006-04-22T18:27:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [April 22, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/how-do-i-manually-throw-a-404/58527/1 "2006-04-22T18:27:51Z")

</div>

I’m sure this has been asked before but a quick search on the web didn’t  
give me any hints…

I’d like to do this:

@person = Person.find\_by\_name  
throw\_page\_not\_found unless @person

Anybody know how to do this, do I have to manually tinker with the  
response object?

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [April 22, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/how-do-i-manually-throw-a-404/58527/2 "2006-04-22T18:43:06Z")

</div>

Jeroen H. wrote:

> 

You could do something like this in app/controllers/application.rb

protected  
def render\_404  
render :file =\> “#{RAILS\_ROOT}/public/404.html”, :status =\> 404  
end

@person = Person.find\_by\_name( ‘meep’ )  
render\_404 unless @person

-Robby

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [April 22, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/how-do-i-manually-throw-a-404/58527/3 "2006-04-22T19:17:12Z")

</div>

Jeroen H. wrote:

> I’m sure this has been asked before but a quick search on the web didn’t  
> give me any hints…
> 
> I’d like to do this:
> 
> @person = Person.find\_by\_name  
> throw\_page\_not\_found unless @person
> 
> Anybody know how to do this, do I have to manually tinker with the  
> response object?

raise ActiveRecord::RecordNotFound or so, look in [api.rubyonrails.com](http://api.rubyonrails.com)  
for the class.

The 404 is in reality triggerd by catching this exception and rendering  
the 404.html file. You could also just do this.

Jonathan

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [April 23, 2006, 9:59am UTC](https://www.ruby-forum.com/t/how-do-i-manually-throw-a-404/58527/4 "2006-04-23T09:59:56Z")

</div>

Jonathan W. wrote:

> Jeroen H. wrote:
> 
> > I’m sure this has been asked before but a quick search on the web didn’t  
> > give me any hints…
> > 
> > I’d like to do this:
> > 
> > @person = Person.find\_by\_name  
> > throw\_page\_not\_found unless @person
> > 
> > Anybody know how to do this, do I have to manually tinker with the  
> > response object?
> 
> raise ActiveRecord::RecordNotFound or so, look in [api.rubyonrails.com](http://api.rubyonrails.com)  
> for the class.
> 
> The 404 is in reality triggerd by catching this exception and rendering  
> the 404.html file. You could also just do this.

This is what I was looking for, I think this is the cleanest way of  
doing things in my situation. I didn’t know it would throw a 404 on a  
RecordNotFound Exception (in dev mode you don’t notice this)

Thanks!

Jeroen
