Dynamic Rails error help

22 Sep 2008 | By Alex Young | Tags programming ruby rails javascript

Here’s a little JavaScript snippet I just wrote for our apps. It parses the standard error messages Rails displays then puts dynamic popup error messages on each of the corresponding form inputs. This allows people to quickly see the error on each field. It currently only displays one error per field, but that could easily be fixed.

You can customise it a bit with by passing a list of options:

  initialize: function(objectName, options) {
    this.options = {
      errorTextSelector: '#errorExplanation ul li',
      messageClass: 'popinfo_inner'
    }

Be aware that the error popup I’m using uses some existing CSS/layout that our apps already use, so you’ll want to customise showError.

Usage:

Instantiate the ModelErrors class with the name of your model:

new ModelErrors('issue')

Download here: model_error.js


blog comments powered by Disqus