https://github.com/chrismaximin/model_error_messages
Ruby gem providing Rails helper, which displays a HTML div with the errors attached to a model.
https://github.com/chrismaximin/model_error_messages
errors gem rails-helper ruby ruby-on-rails
Last synced: over 1 year ago
JSON representation
Ruby gem providing Rails helper, which displays a HTML div with the errors attached to a model.
- Host: GitHub
- URL: https://github.com/chrismaximin/model_error_messages
- Owner: chrismaximin
- License: mit
- Created: 2016-10-25T16:33:27.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-11-21T01:27:53.000Z (over 7 years ago)
- Last Synced: 2025-03-18T19:51:27.365Z (over 1 year ago)
- Topics: errors, gem, rails-helper, ruby, ruby-on-rails
- Language: Ruby
- Size: 17.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Model Error Messages
[](https://travis-ci.org/christophemaximin/model_error_messages)
[](https://rubygems.org/gems/model_error_messages)
[](https://codeclimate.com/github/christophemaximin/model_error_messages)
A simple Rails helper which displays a HTML div with the validation errors attached to a model.
## Install
**Model Error Messages**'s installation is pretty standard, add the following line to your `Gemfile`, then run `bundle install`:
```rb
gem 'model_error_messages'
```
## What this gem does / How to use
This gem allows you to use a helper called `model_error_messages`.
By default, the wrapper `div` has the [Bootstrap](http://getbootstrap.com)-friendly classes `alert alert-danger`.
If you have a typical form, you would want to display `model_error_messages(@model)` next to it, which will render something like:
```html
```
... or if there is only one error:
```html
```
Example of integration:
```erb
<%= model_error_messages(@article) %>
<%= form_for @article do |f| %>
<% end %>
```
## What this gem doesn't do
* Include any other dependencies
* Influence the generation of errors
* Inject or execute any code in your controllers and models
* Do anything with the `flash` messages
* Anything not listed in "What this gem does"
## Optional configuration
You can change the default behavior of `model_error_messages` by:
## Setting an initializer
Create a file `config/initializers/model_error_messages.rb` and replace one of the defaults:
```rb
ModelErrorMessages.configure do |config|
# Multiple errors will rendered in a several
- , while one error will be rendered in a
config.single_error_in_paragraph = true
# The following classes will be added in the main wrapper div.
config.classes = lambda do |model|
[
'alert',
'alert-danger',
model.class.model_name.param_key + '-error-messages'
].join(' ')
end
# Note: you can pass a simple string to `config.classes`, example:
# config.classes = 'alert alert-danger'
# HTML that will be added before the list of errors.
config.prepend_html = String.new
# HTML that will be added after the list of errors.
config.append_html = String.new
end
```
## Passing options when calling `model_error_messages`
Examples:
```erb
<%= model_error_messages(@article, single_error_in_paragraph: false) %>
```
## Contributing
Don't hesitate to send a pull request!
## Testing
```sh
$ bundle install
$ bundle exec rspec spec/
```
## License
This software is distributed under the MIT License. Copyright (c) 2016-2019, Christophe Maximin