Can I have a flash message in the active admin when the destroy failed?

In my application, some resources cannot be destroyed. Therefore, I wrote my model as follows:

before_destroy :destroy_check def destroy_check if some_reason? errors.add(:base, 'cannot destroy this resource!') end errors.blank? end 

Then, when I click the destroy button in ActiveAdmin, do not show anything: there is no error, there is no message, and the record is not actually destroyed. How can I show an error message when the destruction fails?

+7
ruby activeadmin
source share
3 answers

First use the before_destroy model before_destroy to check if the record can be destroyed (here, if the student is signed):

 class Student < ActiveRecord::Base before_destroy :before_destroy_check_for_groups def before_destroy_check_for_groups if StudentInGroup.exists?(student_id: self.id) errors.add(:base, I18n.t('app.student_signed_in')) return false end end end 

It's simple and easy, and you do it for every model you want.

And here is the trick. You can apply a common patch to all Active Admin resources to pass the model error message to the user as a ResourceController callback. The following is the check_model_errors method. And this method should be registered as a callback during the execution of each call to the ActiveAdmin.register method (see Fixed run_registration_block ). You can simply paste the code below into a new file (of any name) in the config/initializers folder of your application (or any other folder that initializes when the application starts). I said this as config/initializers/active_admin_patches.rb .

 class ActiveAdmin::ResourceController def check_model_errors(object) return unless object.errors.any? flash[:error] ||= [] flash[:error].concat(object.errors.full_messages) end end class ActiveAdmin::ResourceDSL alias_method :old_run_registration_block, :run_registration_block def run_registration_block(&block) old_run_registration_block(&block) instance_exec { after_destroy :check_model_errors } end end 
+8
source share

I found that this can be done as part of ActiveAdmin through I18n translations and configure the “Interpolation Settings” in the controller.

Adding the #interpolation_options method to ActiveAdmin :: BaseController in the initializer:

 # config/initializers/active_admin.rb class ActiveAdmin::BaseController private def interpolation_options options = {} options[:resource_errors] = if resource && resource.errors.any? "#{resource.errors.full_messages.to_sentence}." else "" end options end end 

Then, overriding the translation for the kill message in the locale file:

 # config/locales/en.yml en: flash: actions: destroy: alert: "%{resource_name} could not be destroyed. %{resource_errors}" 
+6
source share

if you want to be a resource that has not been removed from the active administrator:

 ActiveAdmin.register SomeModel do controller do def destroy flash[:notice] = 'Cant delete this!' redirect_to :back end end end 

or delete actions:

 ActiveAdmin.register SomeModel do actions :all, except: [:destroy] end 
+3
source share

All Articles