Simple form format a time field to show '21:30' instead of entire UTC time string

时间秒杀一切 提交于 2019-12-11 19:34:11

问题


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?


回答1:


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.




回答2:


You can add attr_accessor to your model (for example formatted_time) and use this field for get fromatted time and set it. Before save you can parse value of @formatted_time and apply it to time_of_event field.



来源:https://stackoverflow.com/questions/17057869/simple-form-format-a-time-field-to-show-2130-instead-of-entire-utc-time-strin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!