Rails time_select设置默认值 [英] Rails time_select set default
本文介绍了Rails time_select设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在rails视图中使用time_select,它看起来像这样:
I am using time_select in a rails view, it looks something like this:
<div class="field">
<%= f.label :StartTime, "Start Time" %><br />
<%= f.time_select :StartTime %>
</div>
我想将默认时间设置为09:00(AM),我该怎么做?
I want to set the default time to 09:00 (AM), how would I do that?
推荐答案
如何设置相应的模型属性?
What about setting corresponding model property?
def new
@session = Session.new
@session.StartTime = Time.now.beginning_of_day + 9.hours
end
请注意,在Ruby中,使用PascalCase命名方法违反了广泛接受的约定。 StartTime
应该变成 start_time
。
Note that in Ruby it's against widely accepted conventions to name methods in PascalCase. StartTime
should become start_time
.
(我把可以自由命名模型 Session
,因为您没有指定任何名称)。
(I took the liberty of naming the model Session
, since you didn't specify any name).
这篇关于Rails time_select设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文