简单的格式化时间字段来显示“21:30”,而不是整个UTC时间字符串 [英] Simple form format a time field to show '21:30' instead of entire UTC time string

查看:164
本文介绍了简单的格式化时间字段来显示“21:30”,而不是整个UTC时间字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My rails _form code:

 <%= simple_form_for @admin_event,:html => {:class => 'main-form'}} do | f | %GT; 
<%= f.input:time_of_event,:format => l(:time_of_event,'%d%b。%Y'),:as =>> :string,:label => 'Hora',:input_html => {:class => 'thin',:placeholder => '例如:20:30'}%>
<%end%>

我收到错误消息:

 不能将符号转换为字符串。 






如何设置 _form 总是使用时间转换器显示此字段,以便数据库包含完整字段( 2000-01-01 19:30:00.000000 ),表格中只显示 19:30

解决方案

<用下面的方法解决了这个问题:在我的ApplicationHelper上创建了方法time_format:

$ p $ def $ time $ form $ date
datetime.strftime('%H:%M')除非datetime.blank?
end

然后在表格中:

 <%= f.input_field:start_time,:as => :string,:value => time_format(f.object.start_time)%> 

希望这能帮到你。


My rails _form code:

<%= simple_form_for @admin_event, :html => { :class => 'main-form' } do |f| %>
  <%= f.input :time_of_event, :format => l(:time_of_event, '%d %b. %Y'), :as => :string, :label => 'Hora', :input_html => { :class => 'thin', :placeholder => 'eg: 20:30' } %>
<% end %>

I'm getting the error message:

Cannot convert symbol to string.


How can I set up this _form to always display this field using a time converter so while the database has a full field (2000-01-01 19:30:00.000000), in the forms it would only show 19:30?

解决方案

Solved this with the following: created the method "time_format" on my ApplicationHelper:

def time_format(datetime)
  datetime.strftime('%H:%M') unless datetime.blank?
end

And then on the form:

<%= f.input_field :start_time, :as => :string, :value => time_format(f.object.start_time) %>

Hope this will help you.

这篇关于简单的格式化时间字段来显示“21:30”,而不是整个UTC时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆