Created
May 18, 2012 14:29
-
-
Save pbjorklund/2725542 to your computer and use it in GitHub Desktop.
Horrible form hack getting all attr_accessible from model
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
| module FormHelper | |
| def errors_for model, field | |
| if model.errors[field].present? | |
| content_tag :span, :class => 'error_explanation' do | |
| model.errors[field].join(", ") | |
| end | |
| end | |
| end | |
| def text_fields_for f, model | |
| #Get the attr_accessible keys from the model | |
| values = model.class.attr_accessible.first[1].to_a.map { |k,v| k } | |
| #Generate a string with html to display in the view | |
| raw(values.inject('') do |sum, el| | |
| sum += f.label el | |
| errors = errors_for model, el.to_sym | |
| unless errors.nil? | |
| sum += errors | |
| sum += f.text_field el.to_s, value: model.send(el) | |
| end | |
| sum | |
| end) | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment