This project is archived and is in readonly mode.

#4048 ✓resolved
Geoff Garside

[PATCH] error_messages_on returning content for methods without errors

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

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.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages