您必须定义一个新的DatePickerInput 类。
module SimpleForm
module Inputs
class DatePickerInput < Base
def input
@builder.text_field(attribute_name,input_html_options)
end
end
end
end
你现在可以写了
<%= f.input :deadline, :as => :date_picker %>
当然你也需要
$("input.date_picker").datepicker();
在application.js
这对于本地化日期非常有用。看看这个:
module SimpleForm
module Inputs
class DatePickerInput < Base
def input
@builder.text_field(attribute_name, input_html_options.merge(datepicker_options(object.send(attribute_name))))
end
def datepicker_options(value = nil)
datepicker_options = {:value => value.nil?? nil : I18n.localize(value)}
end
end
end
end
您现在在文本字段中有一个本地化日期!
更新:一种更简洁的方法
module SimpleForm
module Inputs
class DatePickerInput < SimpleForm::Inputs::StringInput
def input_html_options
value = object.send(attribute_name)
options = {
value: value.nil?? nil : I18n.localize(value),
data: { behaviour: 'datepicker' } # for example
}
# add all html option you need...
super.merge options
end
end
end
end
从SimpleForm::Inputs::StringInput 继承(正如@kikito 所说)并添加一些 html 选项。
如果你还需要一个特定的类,你可以添加类似
def input_html_classes
super.push('date_picker')
end