Customize Devise Timeoutable Length of Time Until Auto Sign Out

The Devise gem is awesome and super useful if you have users logging into your app.  It has a built-in feature for making user sessions automatically signed out or logged out after a certain period of time. Devise calls this :timeoutable.  You can easily customize how long until a user is signed out by going to the config/initializers/devise.rb file and editing the config.timeout_in = 30.minutes line.

  # ==> Configuration for :timeoutable
  # The time you want to timeout the user session without activity. After this
  # time the user will be asked for credentials again. Default is 30 minutes.
  config.timeout_in = 30.minutes # EDIT THIS LINE

So in mine I changed it to

config.timeout_in = 5.minutes

while testing.

Let me know if this was helpful!

Formatting Date in Ruby on Rails

So by default when I used Date.today, the date came out like this 2014/09/05.  However many times you want to display it various formats. This won’t be a comprehensive post on date formatting, but here a few resources that helped me accomplish what I was looking for.

I wanted the date to be formatted like this: Friday, September 5, 2014.  To do this,

Date.today.strftime("%A %B %e %Y ")

You can add commas by simply putting them in the string wherever you want.

Date.today.strftime("%A, %B %e, %Y ")

A very cool website to create your formatting is http://www.foragoodstrftime.com/.  It has an interactive drag and drop code generation tool.

Also some credit here. Other resources include: rails DateTime class, this railscast

Formatting Date and Datetime in Rails

It’s actually pretty easy, and even though the documentation references only show it with datetime, it also seems to work on date objects as well.

So here are the different formats:

 datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0)   # => Tue, 04 Dec 2007 00:00:00 +0000

  datetime.to_formatted_s(:db)            # => "2007-12-04 00:00:00"
  datetime.to_s(:db)                      # => "2007-12-04 00:00:00"
  datetime.to_s(:number)                  # => "20071204000000"
  datetime.to_formatted_s(:short)         # => "04 Dec 00:00"
  datetime.to_formatted_s(:long)          # => "December 04, 2007 00:00"
  datetime.to_formatted_s(:long_ordinal)  # => "December 4th, 2007 00:00"
  datetime.to_formatted_s(:rfc822)        # => "Tue, 04 Dec 2007 00:00:00 +0000"

I used it on a date object with the :long format, and worked like a charm!

credit here