Forum: Ruby on Rails scaffold_resouce destroy

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-03-07 00:02
(Received via mailing list)
Hello group,

I ran across something while creating a client for my RESTful Rails
application.  My client is written in Cocoa (just FYI).

The scaffold_resource generator writes the destory method like this:

  # DELETE /articles/1
  # DELETE /articles/1.xml
  def destroy
    @article = Article.find(params[:id])

    respond_to do |format|
      format.html { redirect_to articles_url }
      format.xml  { head :ok }

During testing I realized that sending a request to delete an object
id that does not exist (or was deleted by another user or process)
results in a fairly ugly 500 - Internal Server Error and responds with
the standard Rails error page HTML even when requesting XML format.

I can detect the status 500 error and report this to the user,
however, I was wondering if there was a cleaner way to respond from
the controller.  Also, would it be useful for the scaffold_resource
generator to be modified to check the result of @article =
Article.find(params[id]) before calling destroy, and respond with a
more appropriate status?  Is there a more appropriate status code for
this operation?

Of course I could make the modifications myself for each controller,
but just wanted to get the opinions of other Rails programmers.
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2007-03-07 00:33
(Received via mailing list)

the reason it's blowing up *should* be because Article.find(params
[:id]) is raising a ActiveRecord::RecordNotFound

That exception will be returned as a 500 while in development mode.

If you're in production mode and hitting a non-local url (not 127.1)
then that exception should automatically be reported as a 404 - which
is exactly what you want.

This topic is locked and can not be replied to.