Ruby on Rails 中的表单输入 type="date"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15913545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:47:44  来源:igfitidea点击:

Form input type="date" in Ruby on Rails

ruby-on-railsruby-on-rails-3ruby-on-rails-4

提问by at.

I want to have a date input for a form. I know this only works in some browsers:

我想为表单输入日期。我知道这仅适用于某些浏览器:

<input type="date" name="birth_day"/>

Is there a convenient method for this in Ruby on Rails? The documentation for the various form elementslist many other input types, but I don't see anything for dates:

在 Ruby on Rails 中有一个方便的方法吗?各种表单元素文档列出了许多其他输入类型,但我没有看到有关日期的任何内容:

  • email_field
  • number_field
  • phone_field
  • range_field
  • search_field
  • telephone_field
  • text_field
  • 电子邮件字段
  • 数字字段
  • 电话字段
  • 范围字段
  • 搜索字段
  • 电话字段
  • 文本域

I know about date_select, that's not what I want. I want the above HTML output. I'm using Ruby on Rails 3.2.13. Is there possibly a date_fieldin Ruby on Rails 4? Or another way to do this?

我知道 date_select,这不是我想要的。我想要上面的 HTML 输出。我正在使用 Ruby on Rails 3.2.13。date_fieldRuby on Rails 4 中可能有吗?或者另一种方法来做到这一点?

回答by Pigueiras

You can see the lastest docsthat the date_fieldhelper provide that. That seems a functionality from Rails 4.

你可以看到最新的文档,该date_field助手提供。这似乎是 Rails 4 的功能。

Example from docs:

来自文档的示例:

date_field("user", "born_on")
# => <input id="user_born_on" name="user[born_on]" type="date" />

回答by kanga

For older versions of Rails I believe you can use:

对于旧版本的 Rails,我相信您可以使用:

.text_field(type: 'date')