https://github.com/umar221b/errors_full_details
Extend ActiveModel::Errors to display messages with error codes.
https://github.com/umar221b/errors_full_details
hacktoberfest rails ruby
Last synced: about 1 year ago
JSON representation
Extend ActiveModel::Errors to display messages with error codes.
- Host: GitHub
- URL: https://github.com/umar221b/errors_full_details
- Owner: umar221b
- License: mit
- Created: 2019-03-28T09:28:55.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-04-17T03:59:11.000Z (about 5 years ago)
- Last Synced: 2025-05-24T17:42:13.610Z (about 1 year ago)
- Topics: hacktoberfest, rails, ruby
- Language: Ruby
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# ErrorsFullDetails
Adds a new method to ActiveModel::Errors to display messages with codes using details.
## Usage
Add errors with codes to an object:
```ruby
class Person < ApplicationRecord
validate :username_is_cool
validate :title_is_looking_good
validate :default_code
private
def username_is_cool
errors.add(:username, 'is not cool', code: 15)
end
def title_is_looking_good
errors.add(:title, 'is not looking good', code: '210')
end
def default_code
errors.add(:base, 'This error has a default code')
end
end
```
Call `object.errors.full_details` to generate an array of error messages and codes:
```ruby
[
{
message: "Username is not cool",
code: "15"
},
{
message: "Title is not looking good",
code: "210"
},
{
message: "This error has a default code",
code: "0"
}
]
```
Errors added without an error code will return `'0'` as a default error code.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'errors_full_details', '~> 0.0.4'
```
And then execute:
```bash
$ bundle
```
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).