Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] error_messages_on returning content for methods without errors

#4048

When using error_messages_on in a form, a <div class="formErrors"></div> element is being rendered into the form for methods without any errors on them.

Given the form

<% form_for @object do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.error_messages_on :name %>
<% end %>

I'm getting this HTML

<form action="/accounts" class="new_account" id="new_account" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="BOt7Iq1ZyBtEYyqbb0DTAj2tNJPYudQA3VmXesekztI=" /></div>
  <label for="account_name">Name</label>
  <input id="account_name" name="account[name]" size="30" type="text" />
  <div class="formError"></div>
</form>

I think this might be related to errors[:name] returning an empty array instead of nil as it used to

Loading development environment (Rails 3.0.0.beta1)
>> Account.new.errors[:name]
=> []

I've attached a patch which replicates this, I'm not sure how it should be fixed though. Either ActiveModel::Errors#[] needs to be patched to return nil for methods without errors or else the ActionView::Helpers::ActiveModelHelper#error_messages_on needs to be changed to check that the errors on the method are not empty.

Reported by Geoff Garside · February 24th, 2010 @ 06:31 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Geoff Garside
    Geoff Garside

    Working on the idea of trying to break the rest of AMo and AP::AV the least I changed ActionView::Helpers::ActiveModelHelper#error_messages_on and it was very simple. Love it when that happens.

    Patch is attached.

    February 24th, 2010 @ 06:41 PM

  2. Geoff Garside
    Geoff Garside

    Additionally now pushed these up to a branch on my fork incase thats easier to apply via

    http://github.com/geoffgarside/rails/tree/error_messages_on

    February 24th, 2010 @ 06:44 PM

  3. Geoff Garside
    Geoff Garside
    • Tag changed from actionview, errors to actionview, errors, patch
    • Title changed from error_messages_on returning content for methods without errors to [PATCH] error_messages_on returning content for methods without errors

    February 24th, 2010 @ 11:09 PM

  4. Geoff Garside
    Geoff Garside

    Sorry just read the contributor guidelines. Attached single file patch with both the test and fix patches integrated.

    February 26th, 2010 @ 09:00 AM

  5. Geoff Garside
    Geoff Garside
    • Tag changed from actionview, errors, patch to actionview, errors, patch, rails3

    March 27th, 2010 @ 09:59 AM

  6. Repository