How to change datetime intervals in Formtastic - ruby-on-rails

Formtastic currently automatically creates a range of 1..59 for datetime inputs.
But what if I wanted the datetime minute options to be 0, 15, 30, 45?
Is there a way I can set the collection for a datetime in Formastic?
Thanks in advance

You can try to use :minute_step => 15 option, for example:
<%= form.input :created_at, :minute_step => 15 %>
Look at select_minute Rails method docs and time_select.
Looks like Formtastic uses Rails select_* methods.

This may be helpful
https://github.com/tamoyal/simple_time_select

Related

time_select helper and Time object creating

There is the following code:
.row
= f.label :start_time
= time_select :model, :start_time, { minute_step: 30 }
.row
= f.label :end_time
= time_select :model, :end_time, { minute_step: 30 }
time_select generates 2 selects for each copy( 1 for hours and 1 for minutes). As result we have 'model' hash in params with 'start_time(1i)', ... 'start_time(5i)' fields (the same for end_time). I have the following questions:
How can I create a new Time object from this hash?
Is it possible to create only one select for each time_select?
Thanks
How can I create a new Time object from this hash?
You can let Rails do the work for you:
= f.time_select(:start_time, minute_step: 30)
When you call update_attributes on your model in the controller, Rails will create the Time object and assign it to the model.
If you use Rails 4, be sure to whitelist :start_time and :end_time using the strong parameters.
Is it possible to create only one select for each time_select?
By default, Rails can't do this. However, you can use the combined_time_select gem to do it for you:
f.time_select(:end_time, combined: true, minute_step: 30)
Be sure to restart your Rails server after installing this gem.
These helpers are optimised for use with a ActiveRecord model and its attributes. If you have the columns start_time and end_time, you should not need to worry about creating the Time object from the passed params yourself.
What are you specifically trying to achieve?

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.
try this
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'!
With the new syntax in Ruby 1.9
f.input :date, as: :date_select, discard_day: true

Rails Time Select, rearrange components

The default rails datetime form builder provides the following form elements as select boxes...
[YYYY] [MONTH] [DD] - [HH]:[MM]
... in 24 hour time.
I would like to present my users with:
[MON.] [DD] [YYYY] - [HH]:[MM] [AM/PM]
How do you do that?
datetime_select helper has lots of different options. I never bothered to remember them all so I use this apidock page as a reference (datetime_select and date_select have similar options).
In you case, you'll need to set :order array, :ampm to true, etc.
Edit: ok, I actually bothered to write the option hash you'll need:
date_select("question", "answered_at", :ampm => true, :order => [:month, :day, :year], :use_short_month => true)

Scaffolding A model with an attribute of type datetime creates a 10 years range in the form

For a simple rails application ( 1.86 /2.3.5) , lets say I run a simple scaffold
script/generate scaffold blog title:string content:text published:date
When I open up the new / edit view for the blog controller in index/new.html.erb , I see that the drop down enabler for date select has a date range of 2005 - 2015 , i.e 5 years +/-
I tried to change this default behavior by introducing this code
f.date_select :entered,
:start_year => 1970,
:end_year => 2020
Apparently this has no impact to the behavior mentioned above. How do I increase the date_select range which seems to be default?
This seems to be ok. Except did you put it inside the <%= %> ?
Edit: Also i noticed that you say your scaffold has published column whereas your date_select refers to the entered column.
As in, <%= f.date_select :published, :start_year => 1970, :end_year => 2020 %>
If that doesn't work you can also try,
<%= f.date_select :published, :start_year => Time.now.year - 40, :end_year => Time.now.year + 10 %>
I tried it and it works for me. Cheers! :)

check if a date is older that the current time in Rails

I am doing a validations in my model to check if a date is older than today (meaning a date can only be in future from the current time)
I was about to write down a block of code, but was wondering is there a build in function for this.
The date is passed from a view using:
<%= date_select ('load', :valid_until, :order => [:day, :month, :year]) %>
All ideas are appreciated.
Yep, there's indeed a built-in date.future? method.

Resources