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!
22 thoughts on “Rails quick tips – How to restrict deleting action and showing flash notice in ActiveAdmin if data have child record”
That’s great
🔔🔔 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
how do i transfer my holdinds to trust wallet before withdraw please
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
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
thanks
https://www.akustiksungerfiyatlari.org
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
METAMUSK team is launching the first token Airdrop of 500kk METAMUSK (≈2.5 BNB). Get on official website –> https://muskmetaverse.me
thank you
https://petokulu.com
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.
Mr Kyle Morgan is a trusted financial advisor. Verified ✅
thank you so much i will call you
Thanks for this useful information.
https://www.bizdekalmasin.com/kategori/coin-gelecegi/
Thanks for an interesting article! As always enjoyed it!
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
Looking for the problem on google and came accross your article. It helped me alot.
Explained really beautifully. And in a very concise manner.
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.
This is an excellent article. This is, in my opinion, one of the best posts ever written. Your work is excellent and inspiring. Thank you very much.
To restrict the deleting action and show flash notice you should use `after_destroy` method. This method is called after the destroy action is performed. This ActiveAdmin notice is for all ActiveAdmin controllers. Just use `after_destroy` and you are done. Credit https://www.klusster.com/portfolios/mo-martin/contents/313136?code=a394cbd5-a58e-492b-ad85-a550b1210b1b&share_content=true
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
What if you had a platform where you could instantly unbox any or all mystery boxes and be guaranteed to win a prize? Not only that, but in addition to exciting prizes and perks, you can get a shopping location to visit right now – today – in the next minute – right now! Yup!
https://www.atoallinks.com/2022/electric-back-to-school-festival-what-can-you-imagine-on-demon-box/