An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# Model Error Messages

[![Build Status](https://secure.travis-ci.org/christophemaximin/model_error_messages.png)](https://travis-ci.org/christophemaximin/model_error_messages)
[![Gem version](https://badge.fury.io/rb/model_error_messages.png)](https://rubygems.org/gems/model_error_messages)
[![Code Climate](https://codeclimate.com/github/christophemaximin/model_error_messages/badges/gpa.svg)](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



  • Title can't be blank

  • You must select an author



```

... or if there is only one error:

```html


Title can't be blank



```

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

  • in a
      , 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