-
Notifications
You must be signed in to change notification settings - Fork 122
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
How to add custom errors when import fails #137
Comments
It would be nice to modify the entire error string, is that possible? |
I also tried to add the error in the def before_import_save(record)
custom_model = CustomModel.find(record[:custom_attribute])
errors.add(:base, "Custom message") unless custom_model
end |
Hi. I don't have a ready made answer for you here. If you are willing to dig into the code of the gem and the rails admin gem to find out where error messages are constructed, and how to add a hook for a custom error message, please open a PR. Make sure to add a paragraph in the README on how to set up custom error messages. |
2 years later, I was trying to achieve the same result and I think I know why it's not working. When calling So when adding an error in a callback like this def before_import_save(record)
# code
errors.add(:base, "Custom message")
# code
end it quits the callback rails_admin_import/lib/rails_admin_import/importer.rb Lines 98 to 106 in 079e929
One solution that I found is to create a dumb attribute on the model to set the error message, and then validate that this attribute is empty. And if not, set this error message on whatever attribute you want. Something like this # add this dumb attribute to your model
attr_accessor :import_error_message
# add a new validation to your model
validate :no_import_error
# somewhere in your before_import_save callback
def before_import_save(record)
# code throwing error
rescue => error
self.import_error_message = error.message # set the dumb attribute to use it later in validations
end
# define this new validation
def no_import_error
if import_error_message # check if there is an error message
errors.add(:base, import_error_message) # add this error on the attribute of your choice, here :base
end
end |
I want to show custom errors when the association is not present or unique.
Rails uniqueness validation adds these errors:
MyAssociation has been taken / MyAssociation must exist
When I try
validates :my_association, uniqueness: {message: "This is my custom message"}
It shows
My Association has been taken. My Association This is my custom message
I am also trying to add other errors not related to validations but to finding a model related to this.
My code:
Probably a Rails-related question but I would love it if you could help in combination with Rails Admin.
Thanks!
The text was updated successfully, but these errors were encountered: