form_for and FormBuilder

0 views
Skip to first unread message

walt

unread,
May 4, 2008, 4:19:13 PM5/4/08
to resources_controller
Hi Ian,

In my lib/ I have an extended_layout_form_builder.rb that I use(d with
resource_controler) on the detail forms like this:

f.render_associated_form(@invoice.invoice_items, :new => 1, :builder
=> ExtendedLayoutFormBuilder)

or, on the many form like this:

<% extended_layout_form_for(:project, :html => { :class =>
"wufoo" }, :url => projects_url) do |f| %>

I've enclosed the contents of my extended formbuilder below.

My question is - how can I make form_for_resource play nice with my
extended layout formbuilder?

(I am aware, that the detail form - render_associated_form - part will
not "fly" with resources_controller, but I sure would like the main
forms to use the extended formbuilder) <:)

Cheers,
walt






--------------

class ExtendedLayoutFormBuilder < ActionView::Helpers::FormBuilder

helpers = field_helpers +
%w(date_select datetime_select time_select collection_select) -
%w(label fields_for)

helpers.each do |name|
define_method name do |field, *args|
options = args.detect {|argument| argument.is_a?(Hash)} || {}
build_shell(field, options) do
super
end
end
end

def build_shell(field, options)
@template.capture do
locals = {
:element => yield,
:label => label(field, (options[:label] || "") ),
:lbl => options[:label],
:frmless => options[:frmless],# || nil,
:required => options[:required],# || nil,
:css => options[:class],
:instruction => options[:instruction]# || nil
}
if has_errors_on?(field)
locals.merge!(:error => error_message(field, options))
@template.render :partial => 'forms/field_with_errors',
:locals => locals
else
@template.render :partial => 'forms/field',
:locals => locals
end
end
end

def error_message(field, options)
if has_errors_on?(field)
errors = object.errors.on(field)
errors.is_a?(Array) ? errors.to_sentence : errors
else
''
end
end

def has_errors_on?(field)
!(object.nil? || object.errors.on(field).blank?)
end

end

walt

unread,
May 5, 2008, 2:37:53 AM5/5/08
to resources_controller
Hi Ian,

well - it's kind of a hack, I think - but here's how I've solved it
(at least for now) :)

<%
extended_layout_form_for( resource.class.name.underscore.to_sym, :html
=> { :class => "wufoo" }, :url => resources_url) do |f| %>

> <% extended_layout_form_for(:project, :html => { :class => "wufoo" }, :url => projects_url) do |f| %>
>

Cheers,
walt
Reply all
Reply to author
Forward
0 new messages