Rails time_select set default

2019-08-16 22:13发布

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>

I want to set the default time to 09:00 (AM), how would I do that?

1条回答
不美不萌又怎样
2楼-- · 2019-08-16 22:26

What about setting corresponding model property?

def new
  @session = Session.new
  @session.StartTime = Time.now.beginning_of_day + 9.hours
end

Note that in Ruby it's against widely accepted conventions to name methods in PascalCase. StartTime should become start_time.

(I took the liberty of naming the model Session, since you didn't specify any name).

查看更多
登录 后发表回答