Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 368
leonel Avatar asked Jan 27 '12 21:01

leonel


3 Answers

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.

like image 70
NicSlim Avatar answered Sep 28 '22 05:09

NicSlim


try this

f.input :accounting_month, :as => :date_select, :discard_day => true 
like image 44
Maysam Torabi Avatar answered Sep 28 '22 05:09

Maysam Torabi


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'!

like image 36
Michael Durrant Avatar answered Sep 28 '22 05:09

Michael Durrant