I have start_time:timestamps
and finish_time:timestamps
fields in events
table. So I want to store here only hours and minutes.
_form.html.erb
<%= f.time_select :start_time_string %>
I added virtual attributes to my model.
event.rb
# Generates methods: start_time_string, finish_time_string
#
[:start_time, :finish_time].each do |field_name|
define_method("#{field_name}_string") do
self.send(field_name).strftime('%H:%M') if self.send(field_name)
end
end
[:start_time, :finish_time].each do |field_name|
define_method("#{field_name}_string=") do |time|
self.send(field_name) = time.strftime('%H:%M')
end
end
But I got error:
syntax error, unexpected '=', expecting keyword_end self.send(field_name) = time.strftime('%H:%M') ^
Why this is happening? Could you please propose a better solution for me if it's possible?