Skip to content

Instantly share code, notes, and snippets.

@pbjorklund
Created May 18, 2012 14:29
Show Gist options
  • Save pbjorklund/2725542 to your computer and use it in GitHub Desktop.
Save pbjorklund/2725542 to your computer and use it in GitHub Desktop.
Horrible form hack getting all attr_accessible from model
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