Skip to content

Instantly share code, notes, and snippets.

@scott-stewart
Created October 7, 2011 02:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scott-stewart/1269328 to your computer and use it in GitHub Desktop.
Save scott-stewart/1269328 to your computer and use it in GitHub Desktop.
SimpleForm Nested Label Component for Twitter Bootstrap CSS
# <rails_root>/lib/simple_form/label_nested_input.rb
module SimpleForm
module Components
module LabelInput
extend ActiveSupport::Concern
included do
include SimpleForm::Components::Labels
end
# Create a html label with nested input and span (containing label text).
# Used to encapsulate the html layout required to support
# Twitter's Bootstrap checkbox and radio button approach.
#
# Example:
#
# <label for="foo">
# <input name="foo" type="hidden" value="0" />
# <input id="foo" name="foo" type="checkbox" value="1" />
# <span>This is the label text</span>
# </label>
#
# Note: A bug somewhere between Rails & HAML prevents the use of
# a block assignment of the content to the label. So, we are
# just verbosely assigning it inline as a method param.
#
def label_nested_input
@builder.label(label_target,
input + template.content_tag('span', label_text),
label_html_options)
end
end
end
end
# include this config/initializers/simple_form.rb
config.wrappers :label_wrapped, :class => "clearfix", :error_class => :error do |b|
b.use :placeholder
b.use :tag => 'div', :class => 'input inputs-list' do |div|
div.use :label_nested_input
div.use :error, :tag => :span, :class => :'help-inline'
end
end
# use in erb/haml like this
= form.input :remember_me, :as => :boolean, :wrapper => :label_wrapped, :label => 'Remember me on this computer'
@i8ramin
Copy link

i8ramin commented Nov 1, 2011

Getting the following error when I try this:
undefined method `label_nested_input' for #SimpleForm::Inputs::BooleanInput:0x007fbf25c67028

Code:
<%= f.input :remember_me, :as => :boolean, :wrapper => :label_wrapped, :label => 'Remember me on this computer' %>

Using:
rails 3.1.1
'simple_form', :git => 'git://github.com/plataformatec/simple_form.git'

@i8ramin
Copy link

i8ramin commented Nov 1, 2011

Figured it out. Had to add this to my simple_form.rb file:
require "simple_form/label_nested_input"

@scott-stewart
Copy link
Author

cool. I was going to have a look at it tonight, but you beat me to the answer :)

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