Skip to content

Instantly share code, notes, and snippets.

@cpm
Created July 30, 2014 23:42
Show Gist options
  • Save cpm/43de2f28b4482e2457c6 to your computer and use it in GitHub Desktop.
Save cpm/43de2f28b4482e2457c6 to your computer and use it in GitHub Desktop.
SimpleForm table wrapper
# place in RAILS_ROOT/config/initializers/
# This initializer will give a simple table-based form layout using SimpleForm
# There are two wrappers provided.
# simple_form_for(@obj, wrapper: "table") will make <TR><TH>label</TH><TD>input, help, and error</TD></TR>
# simple_form_for(@obj, wrapper: "table_just_cells") will skip the TR tags.
SimpleForm.setup do |config|
# wrap all the common wrapper initialization in a lambda
# I don't think you can just do `b.use :table_just_cells` inside :table, or I would have. this works well enough.
table_just_cells_common = ->(b) do
b.use :html5
b.use :placeholder
b.use :label, wrap_with: { tag: 'th' }
b.wrapper tag: 'td', class: 'controls' do |ba|
ba.use :input, wrap_with: { tag: 'div' }
ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
config.wrappers :table_just_cells, tag: false do |b|
table_just_cells_common.(b)
end
config.wrappers :table, tag: 'tr', class: 'simple-form-table-row', error_class: 'error' do |b|
table_just_cells_common.(b)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment