Custom minutes ranges in date input Simple Form - ruby-on-rails

I use SimpleForm for creating forms inside my Rails app. For datetime I use default format from SimpleForm. All is ok, except - I need minutes with gaps, like - 1:00, 1:15, 1:30, 1:45, 2:00.
Is this possible without creating custom input?

Yep. Use code like this:
f.input :time, as: :time, :minute_step => 15

Related

Activeadmin year select has only 10 years before

I have a Date field, date_of_birth
on active admin resource I use
input :date_of_birth, required: true this gives me a dropdown for the year that has only 10 values, from 2016 to 2026.
How can I change the values for year dropdown?
First of all, you might build collection for the date_of_birth. For example (dirty method, I don't love it):
form do |f|
f.inputs do
f.input :date_of_birth, as: :select, collection: YEARS_COLLECTION
end
end
Where YEARS_COLLECTION might be replaced easily with mock (1990..Date.now.year).to_a, or be calculated in your active_admin controller, or might be fetched from the collection (if you have one): BirthYear.all.
Unfortunately, I can't advise anything else because I don't know what years you want to use.

Rails : simple_for_for time with 12 hour format

Is there a proper way to show 12-hour time in Rail's simple_for_for? I am currently using
<%= f.input :time, :ampm => true, :minute_step => 5 %>
however it only displays two drop downs, with the times displayed as 01AM in the first dropdown and 00 in the second.
I would like it to have three drop downs, for hours, minutes, and am/pm.
As per the documentation
HTML 5 date / time inputs are not generated by Simple Form by default, so using date, time or datetime will all generate select boxes using normal Rails helpers. We believe browsers are not totally ready for these yet, but you can easily opt-in on a per-input basis by passing the html5 option:
<%= f.input :time, as: :time, html5: true %>
This should fix your problem.

Date in Rails form

I have a form field in my Rails view:
<%= f.date_select :Date_of_Birth %>
This shows a drop down menu with only the last 10 years and I need to go back to 1800. Better if I could just type the year in rather than have a 200 item drop down list. I also need the option not to fill in the date, that is, blank.
I have found a couple of hints here in stackoverflow, but they don't use quite this format which was generated for me. Suspect because I'm using Rails 4.
Here's how to extend the range of selection:
<%= f.date_select :Date_of_Birth, start_year: 1800, end_year: Time.now.year %>
Having a text input field for the year is not so easy but it could be done, by adding discard_year: true to the date_select.
Then you add a text field tag AFTER the date_select inputs with a special name like name_of_your_object_Date_of_Birth(1i) where "name_of_your_object" is the name you used in the form_for tag. Inspect the HTML fields which date_selects generates in your browser.

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

simple_form input with multiple fields

I'm not quite sure what the correct terms are, but what I'm trying to do is in a form (preferably using the simple_form gem) have one of the inputs, :maximum, use both a text field and select box. The user would type in the text box a number, and then select from a dropdown box of hours, days, or months. So 21 days, 3 months, 3 hours, etc. When the form was submitted I would convert that to days and store it in the database. I know how to change the input type in simple_form, but is it possible to have two inputs for one variable?
Sure :) Here is my idea:
First, you define accessors in your user model:
attr_accessor :thing, :another_thing, :and_another_thing
Then in your view, 'inside' form_for helper, you could write for example:
<%= form.input :thing, :as => :boolean %>
<%= form.input :another_thing, :as => :text %>
...or whatever you want. (Note: I am using formtastic here. You should consider using Rails methods if you're not using formtastic gem. )
Finally, you define a callback in you user model:
before_create :build_my_fancy_record
def build_my_fancy_record
self.storage_field = "#{thing} #{another_thing}"
end

Resources