Skip to content

Instantly share code, notes, and snippets.

@shim0mura
Created December 4, 2012 05:46
Show Gist options
  • Save shim0mura/4201049 to your computer and use it in GitHub Desktop.
Save shim0mura/4201049 to your computer and use it in GitHub Desktop.
FormHelperとFormBuilderを拡張してForm部品を自作する
#coding: utf-8
module ActionView
module Helpers
module FormHelper
def text_field_tag_for_date(object_name, method, options ={})
# インスタンスの取得
object = options[:object]
original_value = object.send(method)
value = original_value.nil? ? "" : original_value.strftime("%Y年%m月%d日")
InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("text", options.update({:value => value}))
end
end
# FormHelperだけだとform_forの中で使えない
# form_forの中でも使えるようにするために、FormBuilderの中にも同じように追加する
class FormBuilder
def text_field_for_date(method, options = {})
# @template: ActionViewのインスタンス
# @object: form_forに渡されたmodelのインスタンス
@template.text_field_tag_for_date(@object_name, method, options.merge(:object => @object))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment