2016-06-22 113 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") %>