-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Comments
The error handling / displaying is not part of AA, it is part of InheritedResources |
@kaspernj, I found this can be accomplished within ActiveAdmin through I18n translations and customizing the Responders' Interpolation Options in the controller. Adding the method # 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}" |
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 |
I will update my own answer a few years later :-) config/initializers/active_admin_monkey_patching.rb
|
@micred Thanks for your patch - It works for me except that I'm getting both flashes
|
Is this something that we should add upstream? |
@vccoffey the record was destroyed? @deivid-rodriguez, I will be happy to open a PR, but I'm not sure if it's ok for you. |
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. |
Destroy errors are not shown in a flash message.
Happens when a model is defined with a
has_many
withrestrict_with_error
as such:The following code can fix it, but I really think this should be the default behavior:
The text was updated successfully, but these errors were encountered: