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!

Twitter
Telegram
Facebook
LinkedIn
Email

21 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

  3. FREE MINING COIN ON PHONE
    REMITANO the largest cryptocurrency exchange in Southeast Asia
    Free RENEC COINS MINING on your phone
    Link Earn RENEC: https://bit.ly/3mDpGn7
    With only 3 steps and 30 seconds a day, you can mine RENEC coin completely free. RENEC coin value can be up to $50/coin

  4. During this period of volatility in the crypto market, we need the help of an expert in the form of a financial advisor on whatsapp +1(208) 7322-211 that specializes in beating the market makers and maximizing profits for clients.

  5. Your article is very good, I have read many articles but I am really impressed with your article. Thank you, I will look into this article. To know about me, try talking to me: retro games

  6. I think it is a topic that will be on the agenda, I will definitely wait for the continuation, and thank you very much for sharing a useful topic.

  7. config/initializers/active_admin_monkey_patching.rb

    Rails.configuration.to_prepare do
    ActiveAdmin::ResourceController::DataAccess.module_eval do
    # Show a notice if destroy is attempted on a resource that has children (e.g. a project with orders).
    def destroy_resource object
    run_destroy_callbacks object do
    object.destroy
    end
    rescue ActiveRecord::DeleteRestrictionError => e
    flash[:error] = e.message
    end

    # Show a notice if destroy is attempted using `accepts_nested_attributes_for`.
    def save_resource object
    run_save_callbacks object do
    object.save
    end
    rescue ActiveRecord::DeleteRestrictionError => e
    flash[:error] = e.message
    end
    end
    end

Leave a Comment

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