嗨,我是RoR的初学者,在制作特定型号时遇到了一些麻烦。
我想创建2个模型 - 列表和项目。 List has_many Items and Item belongs_to List。
我希望Item模型有3个属性。 rails g model Item name:string desc:string date:????
1.要为date:???
2.日期属性的格式是什么? (MM / DD / YY)?
3.它应该有什么样的表格输入?
f.date_field :date
?
提前致谢!
答案 0 :(得分:17)
<强> 1。要为date:???
在迁移中,您可以对列使用以下类型:
:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean
(摘自add_column
转化here)
如果您不需要存储时间,可以使用date:name_of_your_field
。
<强> 2。 date属性的格式是什么? (MM / DD / YY)吗
该属性将存储为ActiveSupport::TimeWithZone
,您必须在显示时将其格式化。您可以使用Time#strftime来执行此操作。
your_attribute.strftime("%m/%d/%Y") #=> "11/19/2007"
第3。它应该有什么样的形式输入?
是的,你可以完美地使用:
f.date_field :date?
它将返回类型为“date”的text_field
。根据浏览器支持,日期选择器将显示在输入字段中。
我希望有所帮助!快乐的编码!
答案 1 :(得分:12)
您可以拥有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
更具描述性的东西。 (只有fyi created_at
和updated_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) %>