How do I manually throw a 404?


#1

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?


#2

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


#3

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
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


#4

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
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