-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Custom Wrappers
You used to have to use a custom wrapper to display the checkbox label beside the checkbox in a vertical form. You can now (as of Dec 2012), declare label: false, and inline_label: true to achieve the same effect (thanks to this helpful StackOverflow post).
<%= f.input :append, as: :boolean, inline_label: true, label: false %>
The section below is helpful for showing how to create a custom wrapper, so has been left as a good description of this.
Here is how to do it as a custom wrapper. Add this to config/initializers/simple_form.rb
config.wrappers :inline_checkbox, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
b.use :html5
b.wrapper :tag => 'div', :class => 'controls' do |ba|
ba.use :label_input, :wrap_with => { :class => 'checkbox inline' }
ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
ba.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' }
end
end
In the view use it like:
= f.input :remember_me, :wrapper => :inline_checkbox
Here is what it looks like:
Using Twitter Bootstrap, I've had luck with the following:
config.wrappers :inline_checkbox, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
b.use :html5
b.wrapper :tag => 'div', :class => 'controls' do |ba|
ba.wrapper :tag => 'label', :class => 'checkbox' do |bb|
bb.use :input
bb.use :label_text
end
ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
ba.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' }
end
end
# config/initializers/simple_form.rb
config.wrappers :inline_checkbox, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
b.use :html5
b.wrapper :tag => 'div', :class => 'controls' do |ba|
ba.use :label_input, :wrap_with => { :class => 'checkbox inline' }
ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
ba.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' }
end
end
// style.css.scss
.form-horizontal {
div.checkbox.inline {
.control-label {
text-align: left;
}
}
}
This page was created by the OSS community and might be outdated or incomplete. Feel free to improve or update this content according to the latest versions of SimpleForm and Rails to help the next developer who visits this wiki after you.
Keep in mind to maintain the guides as simple as possible and to avoid additional dependencies that might be specific to your application or workflow (such as Haml, RSpec, Guard and similars).