AgileVentures / MetPlus_tracker

Git Repository for the Waffle issue in MetPlus project
2 stars 4 forks source link

Show model error messages in standard way #291

Closed patmbolger closed 8 years ago

patmbolger commented 8 years ago

As a developer I want to present model errors in the same way for every form.

patmbolger commented 8 years ago

Currently we have two different ways of presenting model error messages:

1) through code in the application layout view file, and,

2) via the partial views/shared/_error_messages.html.haml.

We should convert all instances of mechanism one to the second mechanism.

joaopapereira commented 8 years ago

In order to make this task easier to implement and to be able to measure the effort, please point out the changes used to move all to 2)

patmbolger commented 8 years ago

To do this, follow these steps for each model to be converted:

1) In the model controller, remove all statements that assign model errors to the var "@model_errors".

2) In the view(s) used for the new and edit actions, add statements at the beginning of the form that invoke the 'shared/error_messages' and assign the object: argument to your model instance. Here is an example from job_seekers/_form.html.haml:

= form_for @jobseeker, html: {class: 'form-horizontal'} do |js|
    .col-sm-offset-2.col-sm-6
        = render 'shared/error_messages', object: @jobseeker
        %br
    .clearfix

3) When all models/controllers have been converted, removed these lines from layouts/application.html.haml:

      -# The following 'if' statement will display model validation errors in the view.
      -# To use this, set the variable @model_errors equal to the errors collection of
      -# your model.  For instance, if your model is Agency and your model instance has
      -# validation errors (perhaps by calling the 'valid?' method) then this line in the
      -# controller would work:
      -#     @model_errors = @agency.errors
      - if @model_errors && @model_errors.any?
        %div{class: 'errors_explanation'}
          %h4= pluralize(@model_errors.count, 'error') + |
               " prevented this record from being saved:" |
          %ul
            - @model_errors.full_messages.each do |msg|
              %li= msg

Note: