Hi there,
I’m trying to create a FormBuilder called LabellingBuilder, to
implement Simply Accessible’s form error messages strategy:
http://simplyaccessible.org/article/form-error-messages
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 http://www.pragmaticprogrammer.com/titles/rails2 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
end
Description
is too short
<%= form.text_area ‘description’ %>
def self.create_tagged_field(method_name)
define_method(method_name) do |label, *args|
@template.content_tag(“div”,
@template.content_tag(“label”,
label.to_s.humanize +
@template.content_tag(“em”,
first_error_message_on(label)),
:for => “#{@object_name}_#{label}”) +
“
” +
super,
:class => “labelled-form-input”)
end
end
field_helpers.each do |name|
create_tagged_field(name)
end
end
#—
Thanks in advance,
Sheldon.