Custom FormBuilder problems

I’m trying to create my first custom FormBuilder and I’m having a bit
of a problem. I’m trying to add a ‘field_group’ method to my builder
that takes a block, but when I yield to the block I seem to be getting
duplicate form field entries being produced. Maybe my code will make
this clearer:

class LabelledBuilder < ActionView::Helpers::FormBuilder
def self.define_labelled_field_method(method_name)
src = <<-end_src
def #{method_name}(label, method, options = {})
hint_text = options[:hint]
@template.content_tag(“tr”) do
@template.content_tag(“td”,
@template.content_tag(“label”,
label.to_s.humanize.titleize, :for => method)) +
@template.content_tag(“td”, super(method, options) +
(hint_text.nil? ? ‘’ : @template.content_tag(“div”,
hint_text, :class => “field_hint”)))
end
end
end_src
class_eval src, FILE, LINE
end

def check_box(label, method, options = {})
super(method, options) +
@template.content_tag(“label”, label, :for => method)
end

def field_group(label, &proc)
raise ArgumentError, “Missing block” unless block_given?
@template.concat(yield, proc.binding) # <— !!! PROBLEM
SEEMS TO BE CAUSED BY THIS YIELD !!!
end

(field_helpers - %w(check_box)).each do |name|
define_labelled_field_method(name)
end
end

If I put this in my view

<% form_for :user, :url => {:controller => :user, :action
=> :new }, :builder => LabelledBuilder do |f| %>

<%= f.text_field "user name:", :user_name, :hint => "Enter user name" %> <% f.field_group "includes:" do %> <%= f.check_box "box", :terms_of_service %> <% end %>
<% end %>

the text field and the checkbox are emitted correctly, but then I also
get an inner element embedded in the outer element, and
the two fields are repeated. Something like this:

<input id="user_terms_of_service" name="user[terms_of_service]"

type=“checkbox” value=“1” />box

User Name:
Enter user name
<input id="user_terms_of_service" name="user[terms_of_service]"

type=“checkbox” value=“1” />box

User Name:
Enter user name

I believe it has something to do with the call to ‘yield’ in my
field_group method, but I’m not sure why this is happening. Can
anybody explain this to me?

TIA,
– bb

I think I figured it out. The problem was related to the fact that I
was calling ‘yield’ within a call to @template.concat, and the block
contained <%= %> blocks, so the erb blocks were omitting the fields
and so was the call to @template.concat. Seems to be working now.

Thanks anyway,
– bb

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs