【问题标题】:How to add a date attribute in rails models如何在 Rails 模型中添加日期属性
【发布时间】:2015-02-03 07:50:54
【问题描述】:

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

我想创建 2 个模型 - 列表和项目。 List has_many Items 和 Item belongs_to List。

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

1.为date:???添加什么数据类型

2.日期属性的格式是什么? (月/日/年)?

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

f.date_field :date?

提前致谢!

【问题讨论】:

    标签: ruby-on-rails ruby activerecord


    【解决方案1】:

    1.为date:???添加什么数据类型

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

    :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean
    

    (提取自add_column转换here

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

    2。日期属性将采用什么格式? (mm/dd/yy)?

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

    your_attribute.strftime("%m/%d/%Y")   #=> "11/19/2007"
    

    3.以及它应该有什么样的表单输入?

    是的,你可以完美使用:

    f.date_field :date?
    

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

    希望对您有所帮助!编码愉快!

    【讨论】:

      【解决方案2】:

      您可以拥有date or datetime according to the documentation. 因此:

      rails g model Item name:string desc:string date:datetime
      

      rails g model Item name:string desc:string date:date
      

      但是 the best practice is to use DateTime as a general purpose representation of time.

      虽然我可能会称它为比date 更具描述性的东西。 (仅供参考,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)
      

      Thanks to @BWStearns for that quote

      最后就输入字段而言:看看these Form helpers

      <%= date_field(:user, :born_on) %>
      <%= datetime_field(:user, :meeting_time) %>
      <%= datetime_local_field(:user, :graduation_day) %>
      

      【讨论】:

      • 感谢您的全面回答!
      • 如何在 html.erb 文件中显示日期? &lt;%= Item.deadline %&gt; 不起作用。这是通过使用deadline:date,而不是deadline:datetime
      • &lt;%= @item %&gt; 应该可以工作。确保将项目分配给实例变量以在视图中看到它(html.erb 文件)。
      • 它是循环的一部分,所以它已经被初始化了!问题是我没有更改我的强参数以在项目控制器中允许:deadline。 :)。再次感谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多