简体   繁体   English

如何在 Rails 中为 datetime_field_tag 设置默认值

[英]How to have a default value for datetime_field_tag in Rails

I am trying to have a default value for datetime_field_tag in my view:我试图在我的视图中为datetime_field_tag设置默认值:

=form_tag form_path, method: :get do
  =label_tag :sign_up_at, 'Sign up before:'
  =date_field_tag :sign_up_at, (params[:sign_up_at] || 
                               Time.zone.today.strftime("%Y-%m-%d"))

  =label_tag :last_seen_at, 'Last seen after:'
  =datetime_field_tag :last_seen_at, (params[:last_seen_at] || 
                                     Time.zone.now.strftime("%d-%m-%YT%H:%M"))

  =submit_tag 'Filter', class: 'btn-info'

This results in having a value tag in HTML:这导致在 HTML 中有一个value标签:

<input type="datetime-local" name="last_seen_at" id="last_seen_at" value="22-05-2017T16:34">

缺少默认值

My guess is, I am having some kind of a problem with parsing Time .我的猜测是,我在解析Time时遇到了某种问题。 Can anyone help, please?有人可以帮忙吗? Thanks!谢谢!

Updated according to @deephak's answer.根据@deephak 的回答更新。

Just to supplement the other answer here: the datetime_field_tag helper is sensitive to the format of the datetime given to it.只是为了补充这里的另一个答案: datetime_field_tag助手对提供给它的日期时间的格式很敏感。 If, for example, the day, month, and year are in the wrong order, the value won't be displayed in the form input (and if you've got the developer console open, a warning message will be displayed).例如,如果日、月和年的顺序错误,则该值将不会显示在表单输入中(如果您打开了开发人员控制台,则会显示一条警告消息)。

It may depend on your locale, but the OP's "%d-%m-%YT%H:%M" format string has the year and day in reverse.这可能取决于您的语言环境,但 OP 的"%d-%m-%YT%H:%M"格式字符串的年份和日期相反。 "%Y-%m-%dT%H:%M" seemed to fix it in my trials. "%Y-%m-%dT%H:%M"似乎在我的试验中修复了它。

You are using today which will give you just the date not time.您正在使用today ,它只会给您date而不是时间。

So, when you try to fetch time out of date rails will return 00:00因此,当您尝试获取过期时间时,rails 将返回00:00

Example:例子:

Time.zone.today
#=> Mon, 22 May 2017

Time.zone.now
#=> Mon, 22 May 2017 09:34:57 EDT -04:00

To fix this, change要解决此问题,请更改

Time.zone.today.strftime("%d-%m-%YT%H:%M")
#=> "22-05-2017T00:00"

to

Time.zone.now.strftime("%d-%m-%YT%H:%M")
#=> "22-05-2017T09:33"

# OR

Time.current.strftime("%d-%m-%YT%H:%M")
#=> "22-05-2017T09:33"

The answers above are correct, but if you would like to refactor this, you can setup a timeformat initializer.上面的答案是正确的,但如果你想重构它,你可以设置一个 timeformat 初始值设定项。

config/time_formats.rb配置/time_formats.rb

Time::DATE_FORMATS[:datetime_field] = '%Y-%m-%dT%H:%M'

Then when setting the value you can use:然后在设置值时,您可以使用:

Time.zone.today.to_s(:datetime_field)

Remember to restart your rails s after changing the initializer.请记住在更改初始化程序后重新启动rails s

You can try你可以试试

date_field_tag(:start_date, value = nil, options = {})

or if you want to submit onchange或者如果你想提交 onchange

date_select(:start_date, value = nil, options = {},onchange: "this.form.submit();")

If you want to try more closer, do like below.如果您想尝试更接近,请执行以下操作。

<%= datetime_field_tag 'start_at_update', Time.zone.now.strftime("%Y-%m-%dT00:00:00"), placeholder: '환율 기간(시작)', class: 'form-control w-50 d-inline-block h-auto w-auto ml-2 value-start-at_update', step: '1' %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM