Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save entrity/5445532 to your computer and use it in GitHub Desktop.
Save entrity/5445532 to your computer and use it in GitHub Desktop.
Create a #date_field helper in your Rails form builder. It is essentially #text_field, but I ended up using this a lot.
module ActionView
module Helpers
class FormBuilder
# Make a field whose class includes 'date' and whose value can be custom-formatted
def date_field method, options={}
date = self.object.send(method).to_date
options[:value] = options.has_key?(:format) ? date.strftime(options[:format]) : date.to_s
options[:class].is_a?(String) ? options[:class] += ' date' : options[:class] = 'date'
rescue
text_field method, options
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment