Enhancement: improved ActiveModal::Errors enumeration

125 views
Skip to first unread message

Evan Prothro

unread,
Feb 29, 2016, 10:46:58 AM2/29/16
to Ruby on Rails: Core

I love the concept and usability of an ActiveModel::Errors paradigm. However, I and others often find it frustrating to work with the implementation.


I'd love to see a proper ActiveModel::Error object, where the errors attribute of a model including ActiveModel::Errors was an Enumerator of these objects.


There are various implied benefits and added flexibility that could be introduced in a backwards compatible way later, and immedate benefits. Would love to know what y'all think about this. Happy to implement if desired.


Immediate Benefits


The error type could be retained

APIs we build prefer to return Error objects to clients with the following schema:

{
  attribute:  "The model attribute to which the error applies. If blank, the error applies generically to the base model.",
  type:       "The type of error encountered with the model attribute or model."
  messsage:   "An internationalized message that can be displayed to a user.",
}

In particular, the type attribute allows clients to couple any logic they need on the error attribute and type, not themessage, which should be able to change with business/product needs.

With a proper ActiveModel::Error object, this is easily done. With the current enumerable design, this is not a clean extension to make.


The error message can be lazily looked up

Currently, the message interpolation is looked up when the error is added to the model. This isn't necessarry, and is easily lazily evaluated when a derived attribute on an Error object.


Other Enhancements easily enabled


Accessing the attribute value more cleanly

error = model.errors.first
error.attribute
=> :username
error.attribute_value
=> 'eprothro'


Easier custom interpolation parameters

# en.yml
errors:
      models:
        user:
          attributes:
            username:
              taken: "%{value} has already been taken."

Rafael Mendonça França

unread,
Feb 29, 2016, 10:58:01 AM2/29/16
to Ruby on Rails: Core
I'm positive for this enhancement. The type information is already retained in Rails 5 but having real object could be real handy.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

ma...@goodlife.tw

unread,
Dec 12, 2017, 7:01:44 AM12/12/17
to Ruby on Rails: Core
I happen to implemented a gem for this, and only now realized someone having the same issue as me.

Repo: https://github.com/lulalala/adequate_errors
Blog post: http://lulalala.logdown.com/posts/2909828-adequate-errors

Would it be possible to take some of these to Rails 6 (with the intention to break compatibility?)

Rafael Mendonça França於 2016年2月29日星期一 UTC+8下午11時58分01秒寫道:

Benjamin Fleischer

unread,
Mar 6, 2018, 11:38:49 AM3/6/18
to Ruby on Rails: Core
Any reason you can't use model.errors.messages ?

ma...@goodlife.tw

unread,
Mar 7, 2018, 1:50:33 AM3/7/18
to Ruby on Rails: Core

You mean using messages to omit attribute prefix?

Because I mostly work with/display full message. Often you have multiple of errors, but only one should be prefix-less, and you want a loop to display them all.

Benjamin Fleischer於 2018年3月7日星期三 UTC+8上午12時38分49秒寫道:
Reply all
Reply to author
Forward
0 new messages