简体   繁体   中英

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.

The datepicker is nice but it shows the whole calendar. I don't want the 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.

试试这个

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. If you use want to store a month-year selection without day you'll see it is a range of dates, not "a" date.

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'. 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. This is definitely a 'hack'!

使用Ruby 1.9中的新语法

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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