问题
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