Rails simple_form 默认输入值不显示
Rails simple_form default input value not showing
我有一个自定义文本输入,我试图在其中显示默认值。该值在 HTML 中设置,但未显示在实际输入中。
我的自定义输入:
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 %>
我也试过没有成功:
<%= f.input :expired_at, as: :date_time_picker, input_html: { value: evaluation.expired_at } %>
我还尝试用一个简单的字符串 "hello"
替换 default_value
方法来测试它是否呈现。它没有。
这个问题已经解决了。问题是我没有告诉输入将数据放入什么格式。
通过在 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" } })
我可以通过合并在 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") %>
我有一个自定义文本输入,我试图在其中显示默认值。该值在 HTML 中设置,但未显示在实际输入中。
我的自定义输入:
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 %>
我也试过没有成功:
<%= f.input :expired_at, as: :date_time_picker, input_html: { value: evaluation.expired_at } %>
我还尝试用一个简单的字符串 "hello"
替换 default_value
方法来测试它是否呈现。它没有。
这个问题已经解决了。问题是我没有告诉输入将数据放入什么格式。
通过在 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" } })
我可以通过合并在 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") %>