如何在rails模型中添加日期属性

dev*_*ast 11 ruby activerecord ruby-on-rails

嗨,我是RoR的初学者,在生成特定模型时遇到了一些麻烦.

我想创建2个模型 - 列表和项目.列表has_many项目和项目belongs_to列表.

我希望Item模型有3个属性. rails g model Item name:string desc:string date:????

1.要添加的数据类型 date:???

2.日期属性的格式是什么?(MM/DD/YY)?

3.它应该有什么样的表格输入?

f.date_field :date

提前致谢!

小智 17

1.要添加的数据类型 date:???

在迁移中,您可以对列使用以下类型:

:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean
Run Code Online (Sandbox Code Playgroud)

(从这里add_column转换中提取)

在您的情况下,如果您不需要存储时间,则可以使用date:name_of_your_field.

2.日期属性的格式是什么?(MM/DD/YY)?

该属性将存储为a ActiveSupport::TimeWithZone,您必须在显示时将其格式化.您可以使用Time#strftime来执行此操作.

your_attribute.strftime("%m/%d/%Y")   #=> "11/19/2007"
Run Code Online (Sandbox Code Playgroud)

3.它应该有什么样的形式输入?

是的,你可以完美地使用:

f.date_field :date?
Run Code Online (Sandbox Code Playgroud)

它将返回text_field"日期"类型.根据浏览器支持,日期选择器将显示在输入字段中.

我希望有所帮助!快乐的编码!


wpp*_*wpp 12

您可以拥有datedatetime根据文档.因此:

rails g model Item name:string desc:string date:datetime
Run Code Online (Sandbox Code Playgroud)

要么

rails g model Item name:string desc:string date:date
Run Code Online (Sandbox Code Playgroud)

一般的想法是使用DateTime作为时间的通用表示.

虽然我可能会把它称为比描述更具描述性的东西date.(并且已经为您创建了fyi created_atupdated_at列.)

该类型几乎与格式无关.你可以用格式化strftime:

%Y%m%d           => 20071119                  Calendar date (basic)
%F               => 2007-11-19                Calendar date (extended)
%Y-%m            => 2007-11                   Calendar date, reduced accuracy, specific month
%Y               => 2007                      Calendar date, reduced accuracy, specific year
%C               => 20                        Calendar date, reduced accuracy, specific century
%Y%j             => 2007323                   Ordinal date (basic)
%Y-%j            => 2007-323                  Ordinal date (extended)
%GW%V%u          => 2007W471                  Week date (basic)
%G-W%V-%u        => 2007-W47-1                Week date (extended)
%GW%V            => 2007W47                   Week date, reduced accuracy, specific week (basic)
%G-W%V           => 2007-W47                  Week date, reduced accuracy, specific week (extended)
%H%M%S           => 083748                    Local time (basic)
%T               => 08:37:48                  Local time (extended)
%H%M             => 0837                      Local time, reduced accuracy, specific minute (basic)
%H:%M            => 08:37                     Local time, reduced accuracy, specific minute (extended)
%H               => 08                        Local time, reduced accuracy, specific hour
%H%M%S,%L        => 083748,000                Local time with decimal fraction, comma as decimal sign (basic)
%T,%L            => 08:37:48,000              Local time with decimal fraction, comma as decimal sign (extended)
%H%M%S.%L        => 083748.000                Local time with decimal fraction, full stop as decimal sign (basic)
%T.%L            => 08:37:48.000              Local time with decimal fraction, full stop as decimal sign (extended)
%H%M%S%z         => 083748-0600               Local time and the difference from UTC (basic)
%T%:z            => 08:37:48-06:00            Local time and the difference from UTC (extended)
%Y%m%dT%H%M%S%z  => 20071119T083748-0600      Date and time of day for calendar date (basic)
%FT%T%:z         => 2007-11-19T08:37:48-06:00 Date and time of day for calendar date (extended)
%Y%jT%H%M%S%z    => 2007323T083748-0600       Date and time of day for ordinal date (basic)
%Y-%jT%T%:z      => 2007-323T08:37:48-06:00   Date and time of day for ordinal date (extended)
%GW%V%uT%H%M%S%z => 2007W471T083748-0600      Date and time of day for week date (basic)
%G-W%V-%uT%T%:z  => 2007-W47-1T08:37:48-06:00 Date and time of day for week date (extended)
%Y%m%dT%H%M      => 20071119T0837             Calendar date and local time (basic)
%FT%R            => 2007-11-19T08:37          Calendar date and local time (extended)
%Y%jT%H%MZ       => 2007323T0837Z             Ordinal date and UTC of day (basic)
%Y-%jT%RZ        => 2007-323T08:37Z           Ordinal date and UTC of day (extended)
%GW%V%uT%H%M%z   => 2007W471T0837-0600        Week date and local time and difference from UTC (basic)
%G-W%V-%uT%R%:z  => 2007-W47-1T08:37-06:00    Week date and local time and difference from UTC (extended)
Run Code Online (Sandbox Code Playgroud)

感谢@BWStearns的报价

最后就输入字段而言:看看这些Form助手.

<%= date_field(:user, :born_on) %>
<%= datetime_field(:user, :meeting_time) %>
<%= datetime_local_field(:user, :graduation_day) %>
Run Code Online (Sandbox Code Playgroud)