The Passionate Craftsman

Ruby, PHP, MySql, Software engineering and more.

Friday, 13 August 2010

When ActiveRecord does not find a record

In Rails, when ActiveRecord does not find a record with the specified ID raises and exception. A good way to control this exception is to intercept it and respond with an appropriate HTTP status code, in this case a 404. This is a simple example:

begin
p = Participant.find params[:id]
render :nothing => true, :status => 200
rescue
render :nothing => true, :status => 404
end
view raw gistfile1.txt hosted with ❤ by GitHub

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home