Forum: Ruby on Rails how do I manually throw a 404?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jeroen H. (Guest)
on 2006-04-22 20:27
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?
Robby R. (Guest)
on 2006-04-22 20:43
(Received via mailing list)
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
Jonathan W. (Guest)
on 2006-04-22 21:17
(Received via mailing list)
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
Jeroen H. (Guest)
on 2006-04-23 11:59
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
This topic is locked and can not be replied to.