I love the simple_form
gem for rails but i dont like this line of code:
<%= f.input :deadline, :as => :string, :input_html => { :class => 'date_picker' } %>
I would like to write:
<%= f.input :deadline, :as => :date_picker %>
or even over write the :date
/ :datetime
matchers completely.
But i dont really want to write a whole custom_simple_form
I think it must be possible...
Please help thanks
Based on @kikito's answer, I did this to get a native datepicker (i.e. no special JS classes).
config/initializers/simple_form_datepicker.rb
Then used it like:
Note that if you also have a simple_form_bootstrap3.rb initializer or similar, like we did, you should:
DatepickerInput
to its list of inputssimple_form_bootstrap3.rb
(or similar) initializer loads aftersimple_form_datepicker.rb
, so that theDatepickerInput
class is available. Do that by e.g. renaming the datepicker initializer tosimple_form_0_datepicker.rb
.An other option could also be to overwrite the default
DateTimeInput
helper, here's an example to be placed inapp/inputs/date_time_input.rb
Please notice than while using this method makes it a drop feature for your forms, it might also break with future versions of simple_form.
Notice about localized dates: As far as I know Ruby interprets dates following only a few formats, you might want to be careful before localizing them and make sure Ruby can handle them. An attempt to better localization support on the Ruby Date parsing as been started at https://github.com/ZenCocoon/I18n-date-parser, yet, this is not working.
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
, notBase
), supports i18n and adds thedatepicker
css class in a more clean way, IMHO.The usage is like above:
And the javascript remains the same:
In new formtastic previews answers doesn't work. See http://justinfrench.com/notebook/formtastic-2-preview-custom-inputs Here is a simple working example (save to app/inputs/date_picker_input.rb):
Create a file app/assets/javascripts/date_picker.js with content:
You also need to load jquery-ui. To do so, insert to app/assets/javascripts/application.js line:
or simply use CDN eg:
Stylesheets for jquery-ui coul be inclded http://babinho.net/2011/10/rails-3-1-jquery-ui/ or from CDN:
You have to define a new
DatePickerInput
class.And you can now write
Off course you also need
in
application.js
This is very useful to localize dates. Look at this:
You have now a localized date in the text field!
Update: a cleaner way to do the same
Inherit from
SimpleForm::Inputs::StringInput
(as @kikito said) and add some html options. If you need also a specific class you can add something like