Forum: Ruby on Rails handling ActiveRecord::RecordNotFound

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.
C7da1c726f89b6b7c37e8a22c0e0382d?d=identicon&s=25 Amita Bhatkhande (amita0204)
on 2009-04-12 18:43
How should we handle requests for invalid objects which do not exist in
database, ActiveRecord::RecordNotFound error ?
I am writing begin-rescue as follows:
  def show
    begin
      @phone = Phone.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      flash[:notice] = NONEXIST_OBJECT
      redirect_to :controller => :phone, :action => :index
    else
      respond_to do |format|
        format.html
        format.xml {render :xml => @agenda.to_xml}
      end
    end
   end

Instead of repeating this for show, edit, and delete in every
controller, I would like to put this into an application_controller. How
do I pass current controller name to the application controller? The
controller and model names are matching, so I would generate a class
name from controller name, then do a begin-find-rescue stuff.

Also, does this belong to controller? My first thought was to handle
this in model, as it is related to ActiveRecord. Any thoughts on this ?

Please help me with putting it into app_con anyway.

~
Amita.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-12 18:54
(Received via mailing list)
On 12 Apr 2009, at 17:43, Amita Bhatkhande wrote:

> this ?
>
If the user asks to see object blah and that doesn't exist, then what
to tell the user is definitely controller material.
If I were you I'd take a look at rescue_from.

Fred
This topic is locked and can not be replied to.