2016-06-22 94 views
2

我有一個自定義文本輸入,我試圖顯示一個默認值。該值被設置在HTML中,但不顯示在實際輸入中。Rails simple_form默認輸入值不顯示

我的自定義輸入:

<%= f.input :expired_at, as: :date_time_picker %>

我自己也嘗試沒有運氣:

class DateTimePickerInput < SimpleForm::Inputs::Base 
 
    def input(wrapper_options) 
 
    template.content_tag(:div, class: "input-group", style: "width: 100%;") do 
 
     template.concat @builder.text_field(attribute_name, input_html_options) 
 
     template.concat span_icon 
 
    end 
 
    end 
 

 
    def input_html_options 
 
    super.merge({ class: 'text datetimepicker form-control', "data-date-input": "", value: default_value }) 
 
    end 
 

 
    def span_icon 
 
    template.content_tag(:span, class: "input-group-addon") do 
 
     template.content_tag(:i, class: "material-icons") do 
 
     "date_range" 
 
     end 
 
    end 
 
    end 
 

 
    def default_value 
 
    object.send(attribute_name).to_time.strftime("%F %I:%M %p") if object.send(attribute_name).present? 
 
    end 
 
end

我打電話的輸入方式

<%= f.input :expired_at, as: :date_time_picker, input_html: { value: evaluation.expired_at } %>

我也嘗試用一個簡單的字符串"hello"替換default_value方法來測試它呈現。它不是。

+0

「值上的HTML設置,但在實際輸入不顯示」。不確定你的意思 - 你能發佈生成的html嗎? –

回答

1

此問題已解決。問題是,我沒有告訴輸入放於數據是什麼格式

溶液中加入以下的input_html_options方法實現:

data: { date_format: "YYYY-MM-DD hh:mm A" }

使該行現在看起來像:

super.merge({ value: default_value, class: "text datetimepicker form-control", data: { date_format: "YYYY-MM-DD hh:mm A" } })

0

我能解決類似的問題,在合併和simple_format's wiki您的解決方案找到了解決辦法。

輸入的樣子:

<%= f.input :my_date, as: :date_time_picker, input_html: { value: @my_entity.my_date.to_time.strftime("%d/%m/%Y") %>