Forum: Ruby on Rails Accessing error messages in a custom FormBuilder

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Sheldon H. (Guest)
on 2007-01-19 17:30
(Received via mailing list)
Hi there,

I'm trying to create a FormBuilder called LabellingBuilder, to
implement Simply Accessible's form error messages strategy:

The problem is that
ActionView::Helpers::ActiveRecordHelper#error_messages_on isn't
available in subclasses of ActionView::Helpers::FormBuilder.

So I tried to write my own little error fetcher and was stunned by the
results -- while @object_name is correctly set, @object is nil, as
indicated by the XXX comment in the code below.

Can anyone help me with this code?

# Based on:
# TaggedBuilder from "Agile Web D. with Rails, 2nd Ed."
# We make no guarantees that this code is fit for any purpose.
# Visit for more
# book information.
class LabellingBuilder < ActionView::Helpers::FormBuilder

  def first_error_message_on(label)
    # XXX @object is always nil
    #errors = @object.errors.on(label)
    #errors.respond_to? :first ? errors.first : errors

  #  <div class="labelled-form-input">
  #    <label for="product_description">
  #      Description
  #      <em>is too short</em>
  #    </label>
  #    <br/>
  #    <%= form.text_area 'description'  %>
  #  </div>
  def self.create_tagged_field(method_name)
    define_method(method_name) do |label, *args|
          label.to_s.humanize +
          :for => "#{@object_name}_#{label}") +
        "<br/>" +
        :class => "labelled-form-input")

  field_helpers.each do |name|


Thanks in advance,
This topic is locked and can not be replied to.