Date Format In Heroku
I'm a total newbie with with Rails and Heroku. I created an app with basic CRUD, which I have deployed to Heroku. A certain entity has a date property. When I create or update with this value:
09/04/2011
It saves as:
04/09/2011
Running locally (with SQLite) I do not see this behavior. I wondered if I needed to be more explicit about culture, so I modified config/locales/en.yml like so:
en:
hello: "Hello world"
date:
formats:
default: "%m/%d/%Y"
This does not seem to have any affect. Ideas?
UPDATE: Here is the controller action:
def create
@entry = Entry.new(params[:entry])
respond_to do |format|
if @entry.save
开发者_开发百科format.html { redirect_to entries_path }
format.xml { render :xml => @entry, :status => :created, :location => @entry }
else
format.html { render :action => "new" }
format.xml { render :xml => @entry.errors, :status => :unprocessable_entity }
end
end
end
Here is the view:
<%= f.text_field :date, :value => (@entry.date.blank? ? '' : @entry.date.strftime('%m/%d/%Y')), :class => 'date' %>
Here is the model:
class Entry < ActiveRecord::Base
end
The locales file only determines how the date is rendered when it is output on the page not how the Db stores it. The Db knows how to store dates it is passed, are you using a date_select to allow the user to pick the date or are you using a text box?
Depending on which version of ruby you're using on Heroku this could be because of Date.parse
assuming EU format (I think this started in 1.9:)
> Date.strptime("09/04/2011", "%m/%d/%y")
=> Fri, 04 Sep 2020
I would check that you're using the same version of ruby for your local development work, if this is the issue, you could override the setter on your attribute:
class Entry < ActiveRecord::Base
def date=(v)
if v.kind_of?(String)
self.write_attribute(:date, Date.strptime(v.to_s, "%m/%d/%y"))
else
super
end
end
end
精彩评论