Last active
August 29, 2015 13:57
-
-
Save biscuitvile/9894638 to your computer and use it in GitHub Desktop.
Basic Form Builder For Simpler Templates
This file contains hidden or 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
| = 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' |
This file contains hidden or 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
| 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