Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroy errors not shown #3335

Closed
kaspernj opened this issue Aug 16, 2014 · 8 comments
Closed

Destroy errors not shown #3335

kaspernj opened this issue Aug 16, 2014 · 8 comments

Comments

@kaspernj
Copy link

Destroy errors are not shown in a flash message.

Happens when a model is defined with a has_many with restrict_with_error as such:

class User
  has_many :orders, dependent: :restrict_with_error
  ..
end

The following code can fix it, but I really think this should be the default behavior:

after_destroy :check_model_errors
controller do
  def check_model_errors(object)
     return unless object.errors.any?
     flash[:error] ||= []
     flash[:error].concat(object.errors.full_messages)
  end
end
@timoschilling
Copy link
Member

The error handling / displaying is not part of AA, it is part of InheritedResources

@zorab47
Copy link
Contributor

zorab47 commented Feb 5, 2015

@kaspernj, I found this can be accomplished within ActiveAdmin through I18n translations and customizing the Responders' Interpolation Options in the controller.

Adding the method #interpolation_options to ActiveAdmin::BaseController in an initializier:

# 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 destroy alert message in the locale file:

# config/locales/en.yml
en:
  flash:
    actions:
      destroy:
        alert: "%{resource_name} could not be removed. %{resource_errors}"

@micred
Copy link

micred commented Jun 17, 2016

Solution by @zorab47 is great but don't cover the evenience when we are trying to destroy a nested object (accept_nested_attributes in the model and f.has_many(:resources, allow_destroy: true) in activeadmin).

As experimental monkey patch I add the following in config/initializers/active_admin.rb:

ActiveAdmin::ResourceController::DataAccess.module_eval do
  def update_resource(object, attributes)
    if object.respond_to?(:assign_attributes)
      object.assign_attributes(*attributes)
    else
      object.attributes = attributes[0]
    end

    begin
      run_update_callbacks object do
        save_resource(object)
      end
    rescue ActiveRecord::RecordNotDestroyed => e
      flash[:error] = "Cannot destroy nested object."
      object.errors.add(:base, e.to_s)
    end
  end
end

@micred
Copy link

micred commented Jul 11, 2022

I will update my own answer a few years later :-)
Now it's compatible with activeadmin 2.11 and rails 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

@vccoffey
Copy link

@micred Thanks for your patch - It works for me except that I'm getting both flashes

Cannot delete record because of dependent [dependent_resource_name]
[resource_name] was successfully destroyed.

@deivid-rodriguez
Copy link
Member

Is this something that we should add upstream?

@micred
Copy link

micred commented Jul 14, 2022

@vccoffey the record was destroyed?
Do you set dependant: restrict_with_exception?
Can you share a code example of your model and steps to replicate it?

@deivid-rodriguez, I will be happy to open a PR, but I'm not sure if it's ok for you.

@deivid-rodriguez
Copy link
Member

Honestly I'm not fully sure where the right place to fix this is, but whatever that is, this seems something that should work by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants