title: Ruby on Rails 3: How to create a custom form element / form field input method?
body: <p>I would like to create a custom form element for Ruby on Rails 3 and package it as a gem. I am coming from a PHP (Zend Framework) background, where it is easy to create custom form elements to use within your application. Being new to Ruby on Rails, I don't know how to start, where to put my code, and which classes to extend. Can you please point me in the right direction? This is the code that I want to end up with:</p>
  
  <pre><code>&lt;%= form_for(@model) do |f| %&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :field_name %&gt;&lt;br /&gt;
      &lt;%= f.my_custom_element :field_name %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>...which will generate some HTML like this:</p>
  
  <pre><code>&lt;div class="field"&gt;
    &lt;label for="model_field_name"&gt;Notes&lt;/label&gt;&lt;br&gt;
    &lt;input type="text" name="model[field_name][field1]"&gt;
    &lt;input type="text" name="model[field_name][field2]"&gt;
    &lt;input type="text" name="model[field_name][field3]"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>..then when the user submits the form, there will be some custom validation, and custom logic to stitch the fields together again into one field before Rails saves the data to the database.</p>
  
  <p>How can I create this custom form element to do this? (By the way, I've been calling it a "custom form element" because that's what it's called in Zend Framework. If Rails has a different name for this, please correct me so I can have an easier time finding information on this subject.)</p>