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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
p = Participant.find params[:id] | |
render :nothing => true, :status => 200 | |
rescue | |
render :nothing => true, :status => 404 | |
end |
Labels: ActiveRecord, rails
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home