Rails quick tips – How to restrict deleting action and showing flash notice in ActiveAdmin if data have child record

image-2

In this post, we are going to share a quick tips about how to restrict deleting action and showing customised flash notice in ActiveAdmin. We use this data model to demonstrate how we do it.


A book has many orders, and we want to restrict deleting action of the book and showing error message in Active Admin if book is accociated to the orders. We have two ways to restrict the delete action, which are using dependent: :restrict_with_error and dependent: :restrict_with_error in your model file.

restrict_with_error

In ActiveAdmin you can use restrict_with_error to restrict deleting action from admin.

class Book < ApplicationRecord
  has_many :orders, dependent: :restrict_with_error
end

But it would not showing the message why we can’t delete the data.

restrict_with_exception

What if we want to show the error message in flash notice, we can use restrict_with_exception to raise an exception and overriding the destroy action in ActiveAdmin to show the message. Here is how we do.

In file app/models/book.rb

class Book < ApplicationRecord
  has_many :orders, dependent: :restrict_with_exception
end

In file app/admin/books.rb

ActiveAdmin.register Book do
  permit_params :title, :author

  controller do
    def destroy
      begin
        resource.destroy
        redirect_to admin_books_path, notice: "Book successfully deleted!"
      rescue ActiveRecord::DeleteRestrictionError => e
        redirect_to resource_path(resource), notice: e.message # can use e.message to get error from Rails or customise your message
      end
    end
  end
end

Then it will show the error message at flash notice

Here is our Ruby on Rails quite tips, see you next time!

Share on twitter
Twitter
Share on telegram
Telegram
Share on facebook
Facebook
Share on linkedin
LinkedIn
Share on email
Email

5 thoughts on “Rails quick tips – How to restrict deleting action and showing flash notice in ActiveAdmin if data have child record”

  1. πŸ””πŸ”” DOUBLE YOUR BITCOIN HOLDING NOW.

    πŸŽ‰THE CRYPTO CLOUD MINING EXCHANGE GRAND REWARD FOR THE CRYPTO COMMUNITY IS LIVE.

    1000 BTC Giveaway to the communityπŸ…πŸ…πŸ….
    To Participate, nominate and verify your Bitcoin wallet now by simply Transferring from 0.03 BTC – 5 BTC. And you will instantly receive double of the transfered value. (0.09 BTC – 15 BTC) back.

    COMMUNITY ADDRESS: 33bxVT3ZfbDzQ15rtNmpMNn1yBmXyXf7sk

      1. Sekarang ni mung pakai aave#nolongeravialable busy lah dengan Anal in she’s mom care. ( analisis market).. tido pun sekejap jer.. itu lah Head of Coin ( stock)’- crypto = ekor ( tail ) Coin ekor.
        – hantu

  2. Coingecko staff-
    Our price analysis is 12%, aave.. I dont know. Saya bukan aave. Saya coingecko.

    Pasal aku x pernah merasa happy dengan kebajikan Aave.com.

    Sekiranya ada harga coingecko harap2 jadi macam aava… main batu pelir jer.. tiadA pun nak tanya aku ni dah makan ke belum.. aave meninggalkan aku keseorangan..

    Kerala bukan aku, bukan anak raja malaysia.. x layak UNTUK aku menumpang kasih.. argh… pergilah aave bersama dia yang aku suka.. pergi sahaja. Berambus dari hidup aku.
    Kerana tiadA yang datang kerumah aku memberitahu.. semuanya cakap tamil air biru… ManA aku dalam sahaja air biru

Leave a Comment

Your email address will not be published. Required fields are marked *