Created
November 20, 2012 01:09
-
-
Save rjz/4115293 to your computer and use it in GitHub Desktop.
Side-by-side fields for formtastic-bootstrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using Rails' flat form builder is not the most OO-approach, but it fits the bill for a quick+dirty!