jueves, 13 de agosto de 2009

ActiveRecord::StatementInvalid customize messages

I tried to customize the active record error messages on Rails project, and I was boring to find the answer, the simple way of have my personal messages is use a begin rescue block, an example is above, I am working with RoR 1.26 and PostgreSQL.

The function is delete_row and is in application.rb (Global controller)


#---- written by Carlos Augusto Monterrosa Lopez
def delete_row(row)
  begin
    row.destroy
    flash[:notice]="Row deleted"
    redirect_to :action => 'list', :controller => "#{params[:controller]}"
  rescue
    flash[:notice]="Here you personal error message"
    redirect_to :action => 'list', :controller => "#{params[:controller]}"
  end
end