Reputation: 137
I'm working on storing a non-default variable when a user clicks a certain button. the model is Team and the variable is start_time.
I'm currently setting the Time in a AJAX form using the below form.
<%= form_for @team, remote: true do |f| %>
<%= f.hidden_field :start_time, :value => Time.now.in_time_zone("Eastern Time (US & Canada)") %>
<div class="actions">
<%= f.submit 'Begin Exercise Set Up', :class => "btn btn-primary" %>
</div>
<% end %>
However, the time being recorded in the DB is not the current time, but the time at which the page loaded. I have tried the above code as well as Time.now. There seems to be very little documentation on this specific issue online and I cannot get this to work.
Here is the local web server log during the Model Post.
(0.2ms) BEGIN
(0.6ms) UPDATE "teams" SET "start_time" = '2013-07-08 00:17:44.000000', "updated_at" = '2013-07-08 00:17:50.843328' WHERE "teams"."id" = 59
(141.4ms) COMMIT
You can see the start_time value does not equal the real time that updated_at is setting.
In addition, neither of these values are equivalent to the value of my local clock on my Macbook pro.
Thanks for the help everyone.
Upvotes: 0
Views: 1146
Reputation: 6961
Your start_time
(Time.now.in_time.zone
) is populated on the page load/render. Whereas the updated_at
is re-populated when you save the model. Your times are only off by a few seconds, which indicates that it took the person a few seconds to submit the form.
Based on the updated requirement of it being when a start button is clicked to submit a form, it sounds like you want to populate the time in the controller action.
def update
# your code
team.start_time = Time.now.in_time_zone('EST')
# rest of your code saving and processing the return
end
As for why the time is different, it should not be, unless you are in a different timezone. However, I did notice there is no timezone in the SQL generated. It is possible that the database is not timezone aware and things are being converted to UTC. What is your DB?
Upvotes: 1
Reputation: 7078
1 . You set a static time in the view witch gets generated on page load.
2 . Set the time in the controller when you save the object. Basic example:
@object = Object.new(params[:object].merge(:start_time => Time.now))
if @object.save
redirect_to 'best side in da world'
else
render :new
end
Upvotes: 0