work with rescue in Rails

Posted by Adnan on Stack Overflow See other posts from Stack Overflow or by Adnan
Published on 2010-04-02T14:28:10Z Indexed on 2010/04/02 14:33 UTC
Read the original article Hit count: 284

Filed under:
|
|

Hi,

I am working with the following piece;

def index
  @user = User.find(params[:id]) 
  rescue
    flash[:notice] = "ERROR"
    redirect_to(:action => 'index')
  else 
    flash[:notice] = "OK"
    redirect_to(:action => 'index')
end

Now I either case whether I have a correct ID or not, I am always getting "OK" in my view, what am I doing wrong?

I need that when I have no ID in the DB to show "ERROR". I have also tried to use rescue ActiveRecord::RecordNotFound but same happens.

All help is appreciated.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about rescue