Skip to content

Instantly share code, notes, and snippets.

@rjz
Created November 20, 2012 01:09
Show Gist options
  • Save rjz/4115293 to your computer and use it in GitHub Desktop.
Save rjz/4115293 to your computer and use it in GitHub Desktop.
Side-by-side fields for formtastic-bootstrap
# config/initializers/formtastic.rb
module FormtasticBootstrap
class FormBuilder < Formtastic::FormBuilder
# Allow "split-field" inputs such as name (first+last) or location (city+state).
# Block contents are wrapped in a ".controls" field set next to the specified
# +label+
#
# Usage:
#
# <%= f.split_fields 'Name' do %>
# <%= f.text_field :first_name, :placeholder => 'First', :style => 'width:80px' %>
# <%= f.text_field :last_name, :placeholder => 'Last', :style => 'width:100px' %>
# <% end %>
#
def split_fields(label, &block)
if block_given?
controls = template.capture(&block)
label = template.content_tag :div, label, :class => 'control-label'
controls = template.content_tag :div, controls, :class => 'controls'
template.content_tag(:div, label + controls, :class => 'control-group')
end
end
end
end
Formtastic::Helpers::FormHelper.builder = FormtasticBootstrap::FormBuilder
@rjz
Copy link
Author

rjz commented Nov 21, 2012

Using Rails' flat form builder is not the most OO-approach, but it fits the bill for a quick+dirty!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment