Heroku 中的日期格式
我是 Rails 和 Heroku 的新手。我创建了一个具有基本 CRUD 的应用程序,并将其部署到 Heroku。某个实体具有日期属性。当我使用此值创建或更新时:
2011年9月4日
它另存为:
2011年4月9日
在本地运行(使用 SQLite)我没有看到这种行为。我想知道我是否需要更明确地了解文化,所以我修改了 config/locales/en.yml,如下所示:
en:
hello: "Hello world"
date:
formats:
default: "%m/%d/%Y"
这似乎没有任何影响。有想法吗?
更新:这是控制器操作:
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
这是视图:
<%= f.text_field :date, :value => (@entry.date.blank? ? '' : @entry.date.strftime('%m/%d/%Y')), :class => 'date' %>
这是模型:
class Entry < ActiveRecord::Base
end
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
locales 文件仅决定日期在页面上输出时如何呈现,而不决定数据库如何存储它。数据库知道如何存储传递的日期,您是使用 date_select 来允许用户选择日期还是使用文本框?
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?
根据您在 Heroku 上使用的 ruby 版本,这可能是因为
Date.parse
假设采用 EU 格式(我认为这是从 1.9 开始的:)我会检查您是否使用相同的版本ruby 用于本地开发工作,如果这是问题,您可以覆盖属性上的设置器:
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:)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: