简体   繁体   中英

Simple form. How to override input to be text_field?

I am using simple_form gem and I need to change the type of "created_at" field from input to text_field (in order to use this field with jQuery date picker). Is it possible?

as: :string

is not suitable answer since it is not working with jQuery.datepicker

It is needed to create specific DatePickerInput class in app/inputs/date_picker.rb

app/inputs/date_picker.rb

class DatePickerInput < SimpleForm::Inputs::StringInput 
  def input                    
    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

javascript

jQuery -> 
 $('input.date_picker').datepicker()

in form just add the following line

    = f.input :due, as: :date_picker

This thread solved my problem How do i write a cleaner date picker input for SimpleForm thanks to rio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM