Timezone parsing fails for JSON requests in test environment #7588

adamstrickland opened this Issue Sep 10, 2012 · 3 comments


None yet
3 participants

Recently upgraded from 3.2.1 to 3.2.8 and found that something changed. Controller specs POSTing a hash containing :format => :json are no longer stringified, causing Time.zone.parse to fail with

undefined method 'sub' for Mon, 10 Sep 2012:Date


In spec:

it "should not fail" do
  attrs = @attributes.merge(:format => :json)    # <= hash without :format => :json has no problems
  post :create, attrs

In controller:

def create
  params[:thing][:date_field] = tz.parse(params[:thing][:date_field]).utc  # <= fails here
  @model = Thing.new(params[:thing])

Tracked down to Date.convert_american_to_iso(string#Date). Snip:

# Transform american date fromat into ISO format.
def convert_american_to_iso(string)
  string.sub(AMERICAN_DATE_RE){|m| "#$3-#$1-#$2"}

where the method is clearly expecting a string, but instead is getting a Date object. In other words, params[:thing][:date_field] is a Date, not a String, but only in the case when the paramter hash is annotated as JSON.

I am currently using a workaround in my specs:

@attributes[:thing] = @attributes[:thing].map{ |k, v| {k => v.to_s} }.reduce({}, :update)
post :create, @attributes.merge(:format => :json)

vipulnsward commented Aug 26, 2013

@adamstrickland can you confirm if this is still an issue against master?


senny commented Feb 4, 2014

@adamstrickland It's been another 5 month's without feedback. If you still experience this problem, please report back and provide an executable test-case or a sample application. This will help us to debug the issue.

senny closed this Feb 4, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment