title: How to extend a core Rails FormBuilder field
body: <p>I am using Bootstrap 3 with Rails 4, and I wanted to create a custom FormBuilder to handle some of Bootstrap's unique HTML syntax. Specifically, I needed a custom helper that would create the <code>form-group</code> div wrapper around a form field, since <a href="http://getbootstrap.com/css/#forms-control-validation">Bootstrap applies error state to this wrapper, and not the field itself</a>...</p>
  
  <pre><code>&lt;div class="form-group has-error"&gt;
    &lt;label class="col-md-3 control-label" for="user_email"&gt;Email&lt;/label&gt;
    &lt;div class='col-md-9'&gt;
      &lt;input class="form-control required-input" id="user_email" name="user[email]" placeholder="peter@example.com" type="email" value="someone@example.com" /&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Note the extra class <code>has-error</code> in the outer div...</p>
  
  <p>Anyway, I wrote that helper, and it works great!</p>
  
  <pre><code>def form_group(method, options={})
    class_def = 'form-group'
    class_def &lt;&lt; ' has-error' unless @object.errors[method].blank?
    class_def &lt;&lt; " #{options[:class]}" if options[:class].present?
    options[:class] = class_def
    @template.content_tag(:div, options) { yield }
  end
  
  # Here's a HAML sample...
  
  = f.form_group :email do
    = f.label :email, nil, class: 'col-md-3 control-label'
    .col-md-9
      = f.email_field :email, class: 'form-control required-input', placeholder: t('sample.email')
  </code></pre>
  
  <p>Now I want to utilize Bootstrap's <a href="http://getbootstrap.com/css/#forms-help-text">form help text</a> in order to display error messages. This requires me to extend Rails native helpers (such as <code>text_field</code> in the example above) and then call them within the the block of <code>f.form_group</code>.</p>
  
  <p>The solution <strong>seemed</strong> simple enough: call the parent, and append my span block onto the end...</p>
  
  <pre><code>def text_field(method, options={})
    @template.text_field(method, options)
    if !@object.errors[method].blank?
      @template.content_tag(:span, @object.errors.full_messages_for(method), class: 'help-block')
    end
  end
  </code></pre>
  
  <p>Only it wouldn't output any HTML, the div would simply show up empty. I've tried a bunch of diff syntax approaches:</p>
  
  <ul>
  <li><code>super</code> vs <code>text_field</code> vs <code>text_field_tag</code></li>
  <li><code>concat</code>-ing the results -- <code>@template.concat(@template.content_tag( [...] ))</code></li>
  <li>dynamic vars, e.g. <code>def text_field(method, *args)</code> and then <code>options = args.extract_options!.symbolize_keys!</code></li>
  </ul>
  
  <p>I only ever get weird syntax errors, or an empty <code>div</code>. In some instances, the <code>input</code> field would appear, but the help text <code>span</code> wouldn't, or vice verse.</p>
  
  <p>I'm sure I'm screwing up something simple, I just don't see it.</p>