ruby-on-railsruby-on-rails-7trestle-admin

Trestle Admin Custom Delete


i am new to rails & using Trestle Admin as an easy backend solution for my app.

When i try to delete an item in the trestle admin backend, i get following error:

PG::ForeignKeyViolation: ERROR: update or delete on table "AAA" violates foreign > key constraint "fk_rails_xxxxxx" on table "BBB" DETAIL: Key (id)=(2) is still referenced from table "BBB".

Thats fine but instead of showing an application error i would like to check for the error and display a custom alert message. I don't know how this is possibly with trestle.. Has anyone an idea how to archive this?

Thanks


Solution

  • I figured out that i simply can do something like this, which fixed my issue:

    controller do
     def index; end
    
     def destroy
       if SecondModal.where(xxx_id: params[:id]).length > 0
         flash[:error] = "Record was not destroyed, because it's still referenced in other tables"
         redirect_to admin.path(:index)
       else
         FirstModal.delete(params[:id])
         flash[:success] = 'Record destroyed'
         redirect_to admin.path(:index)
       end
     end
    end