lando2319
lando2319

Reputation: 1810

DateTime.now not setting timezone in Rails

I have a column called "Archived" in my message table and a button that when pushed will write the current time and date into the db column. Right now I can get it to write the time, however it's in the wrong timezone. After looking online I created the following code which is currently saving, but not in the correct timezone:

Controller Code

def archive_message
  @message = Message.find(params[:id])
  @message.archived = DateTime.now.in_time_zone "Central Time (US & Canada)"
  @message.save

  redirect_to '/messages'
end

My route (I believe this is ok as is)

post 'messages/:id/archive_message' => 'Messages#archive_message', :as => 'archive_message'

Any suggestions are well appreciated.

Upvotes: 2

Views: 980

Answers (1)

lando2319
lando2319

Reputation: 1810

EDIT AGAIN: OK this is UTC time, so it's working. Thank you everyone.

EDIT: I just closed an restarted everything and now it is NOT WORKING, it is saving the time as 5 hours ahead again and I have no idea why.

In config/application.rb added the following to module code

config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = :local

and my controller

@message.archived = DateTime.current

thanks everyone

Upvotes: 1

Related Questions