Created
December 4, 2012 05:46
-
-
Save shim0mura/4201049 to your computer and use it in GitHub Desktop.
FormHelperとFormBuilderを拡張してForm部品を自作する
This file contains 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
#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