简体   繁体   English

Rails 3 Formtastic。 如何在没有白天的情况下仅将月份和年份字段显示为formtastic?

[英]Rails 3 Formtastic. How can I make formtastic display only the month and year fields WITHOUT the day?

I need formtastic to display only month and year fields, WITHOUT the day fields. 我需要formtastic才能显示月份和年份字段,没有日期字段。

The datepicker is nice but it shows the whole calendar. datepicker很不错,但它显示了整个日历。 I don't want the datepicker. 我不想要datepicker。
f.input :accounting_month, :label => "Accounting month", :as => :datepicker

All I need is the month and year. 我需要的只是月份和年份。

There is a way to do this: 有一种方法可以做到这一点:

<%= f.input :accounting_month, :label => "Accounting month", :order => [:month, :year]

This will automatically hide the "day" input and give it a default value of 1. 这将自动隐藏“日期”输入并为其指定默认值1。

试试这个

f.input :accounting_month, :as => :date_select, :discard_day => true 

It doesn't work that way because without day this implies 28-31 (depending on month) days and it could be any one of them. 它不起作用,因为没有一天这意味着28-31(取决于月份)天,它可能是其中任何一个。 If you use want to store a month-year selection without day you'll see it is a range of dates, not "a" date. 如果您使用想要存储没有日期的月份选择,您将看到它是一系列日期,而不是“a”日期。

Advice with dates is to always store the whole date as a date field. 对日期的建议是始终将整个日期存储为日期字段。 If you only know month and year you'll need to have two (non-date) fields, one for each. 如果您只知道月份和年份,则需要有两个(非日期)字段,每个字段一个。 But as you can see it's going to lose you a lot and you'll need to custom craft each field, validate it, etc. major pain so needs pretty good reason to do it.. 但正如你可以看到它会失去你很多,你需要定制工艺每个领域,验证它,等主要的痛苦,所以需要很好的理由去做..

The only other thing I can suggest is: create a hidden div, or apply css if you can't 'get inside' the standard date field on the form, for the 'day'. 我可以建议的另一件事是:创建一个隐藏的div,或者如果你不能“进入”表单上的标准日期字段,那么应用css为'day'。 Then set the value to always be 01. Then you might have a 'date' set of fields that will save 01-month-year to the database. 然后将值设置为始终为01.然后,您可能有一组“日期”字段,将01个月的年份保存到数据库中。 This is definitely a 'hack'! 这肯定是'黑客'!

使用Ruby 1.9中的新语法

f.input :date, as: :date_select, discard_day: true

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

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