The answers here seem a bit out of date if you are using simple_form 2.0.
I've been fighting with this for a while and was able to concoct this; it uses inheritance (notice that it is a subclass of StringInput
, not Base
), supports i18n and adds the datepicker
css class in a more clean way, IMHO.
# app/inputs/date_picker_input.rb
class DatePickerInput < SimpleForm::Inputs::StringInput
def input
value = input_html_options[:value]
value ||= object.send(attribute_name) if object.respond_to? attribute_name
input_html_options[:value] ||= I18n.localize(value) if value.present?
input_html_classes << "datepicker"
super # leave StringInput do the real rendering
end
end
The usage is like above:
<%= f.input :deadline, :as => :date_picker %>
And the javascript remains the same:
$("input.date_picker").datepicker();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…