[英]How to set default date in date_select helper in Rails
I'm trying to set up a date of birth helper in my Rails app (2.3.5). 我正在尝试在我的Rails应用程序(2.3.5)中设置出生日助手的日期。 At present it is like so.
目前它是这样的。
<%= f.date_select :date_of_birth, :start_year => Time.now.year - 110, :end_year => Time.now.year %>
This generates a perfectly functional set of date fields that work just fine but.... 这会生成一组功能完善的日期字段,但工作正常但......
They default to today's date which is not ideal for a date of birth field (I'm not sure what is but unless you're running a neonatal unit today's date seems less than ideal). 它们默认为今天的日期,这对于出生日期并不理想(我不确定是什么,但除非你今天运行新生儿单位的日期似乎不太理想)。 I want it to read Jan 1 2010 instead (or 2011 or whatever year it happens to be).
我想让它改为2010年1月1日(或2011年或其他任何年份)。 Using the :default option has proven unsuccessful.
使用:default选项已证明不成功。 I've tried many possibilities including;
我尝试了很多可能性,包括;
<%= f.date_select :date_of_birth, :default => {:year => Time.now.year, :month => 'Jan', :day => 1}, :start_year => Time.now.year - 110, :end_year => Time.now.year %>
and 和
<%= f.date_select :date_of_birth, :default => Time.local(2010,'Jan',1), :start_year => Time.now.year - 110, :end_year => Time.now.year %>
None of this changes the behaviour of the first example. 这些都不会改变第一个示例的行为。 Does the default option actually work as described?
默认选项实际上是否按照描述工作? It seems that this should be a fairly straightforward thing to do.
看起来这应该是一件相当简单的事情。
Ta. 助教。
I think that easiest way is to assign it in controller, for example for new action: 我认为最简单的方法是在控制器中分配它,例如用于新操作:
@person = Person.new(:date_of_birth => "2010-01-01".to_date)
Then in your view you will get correct date. 然后在您的视图中,您将获得正确的日期。
EDIT: 编辑:
If you want to assign default value on model level, you can try do it with plugin . 如果要在模型级别指定默认值,可以尝试使用插件执行此操作。 However I haven't try it.
但是我没试过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.