Skip to content

Instantly share code, notes, and snippets.

@biscuitvile
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save biscuitvile/9894638 to your computer and use it in GitHub Desktop.

Select an option

Save biscuitvile/9894638 to your computer and use it in GitHub Desktop.
Basic Form Builder For Simpler Templates
= form_for [:admin, @account], url: :admin_settings, class: 'settings' do |f|
%section.panel.panel_form
= f.wrapper :store_name do
= f.label :store_name
= f.text_field :store_name
= f.wrapper :description, :textarea do
= f.label :description
= f.text_field :description
= f.wrapper :contact_email do
= f.label :contact_email
= f.text_field :contact_email
%section.panel
%p.input.text
= f.submit t('admin.settings.save'), class: 'action_button success'
class FormBuilder < ActionView::Helpers::FormBuilder
def label(attribute, text = nil, options = {}, &block)
text = (text || attribute.to_s.humanize.titleize)
unless object.nil?
errors = object.errors[attribute.to_sym]
if errors.present?
text += " #{errors.is_a?(Array) ? errors.first : errors}"
end
end
super(attribute, text.html_safe, options, &block)
end
def wrapper(attribute, kind = :text, &block)
html_class = 'input ' + kind.to_s
html_class += ' input_focus' if object.public_send(attribute).present?
html_class += ' input_error' if object.errors[attribute.to_sym].present?
@template.content_tag "p", class: html_class, &block
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment