Postgresql change the year range and input order - ruby-on-rails

I have a date_of_birth field with type date and I need to correct the following two issues:
The year range is limiting to 2010..2020
The order of the fields is YYYY - MM - DD and I would like it to be MM - DD - YYYY

I was able to get the desired output as follows (strictly with simple_form):
<%= f.input(:date_of_birth, start_year: 1900, end_year: Time.now.year - 1, order: [:month, :day, :year]) %>

Related

How to manipulate date_select to show customized months? Rails

I need my date select form field to show users only the future dates (like for eg. today is 7 apr 2016, then it must show all dates beyond 7 apr 2016) and for month field it must only show the current month,year(any)
<%= f.date_select :start_date,class: 'form-control'%>
Year: any
Date: only future dates
Month: the current ongoing month
There are two ways:
First is that, you validate the date before saving it, at model level. If date is note in future don't save and return error.
Second, you can use jQuery Date Picker and use its minDate option: http://api.jqueryui.com/datepicker/#option-minDate
Using date_select you only have the option of restricting the starting year but not month and day. http://apidock.com/rails/v3.2.1/ActionView/Helpers/DateHelper/date_select
<%= f.date_select :start_date, start_year: Date.today.year, class: 'form-control'%>

How to set default date and include_blank in date_select in Rails

I'm using date_select in Rails 4 and I'd like to be able to have today's date as default but also allow for the user to leave the date field blank.
<%= f.date_select(:birthdate, {include_blank: true, default: Date.today.to_date, start_year: Date.today.year - 100, end_year: Date.today.year - 18}) %>
What is the best way to allow for blank as well as have today's date appear by default when the page opens?
The option you need is selected, i.e:
<%= f.date_select(:birthdate,
include_blank: true,
selected: Date.today, # substitute 18.years.ago to prefill year
start_year: Date.today.year - 100,
end_year: Date.today.year - 18) %>
The approved answer will overwrite the value already assigned to the birthdate every time you go to edit the record. See here.
If your model is, e.g., #student, and your form is form_for #student do |f|, this should work. It fails through to Date.current if #student.birthdate is empty.
<%= f.date_select(:birthdate,
include_blank: true,
selected: #student.birthdate || Date.current, # Date.current respects time_zones
start_year: Date.today.year - 100,
end_year: Date.today.year - 18) %>
However, be aware that the blank option will always be set to the current date when you go to edit the record, requiring the user to reset to blank if the data is unknown. This risks the insertion of bad data - arguably, less usable than having to choose the date to begin with.

Rails - how to validate one field against another for dates

I think this is a simple question. In my app, I have a model where users select a 'start' date and a 'stop' date when submitting a new record. The dates are only the years, excluding month and day.
I just want to write a validation to ensure that the stop date is greater than the start date, so you can't submit something that started in 2012 and ended in 2008. How would I do that?
My form fields are below:
<%= select_year Date.today, start_year: Time.now.year, end_year: Time.now.year - 95, field_name: :start %>
<%= select_year Date.today, start_year: Time.now.year, end_year: Time.now.year - 95, field_name: :stop %>
If you want to do a server side validation, you could add validation to your model
validate :stop_date
def stop_date
errors.add(:stop, "stop date cannot be older than start date") if stop < end
end
You could populate the errors on the view.

How to stop rails from including current day and month in date helper

I'm trying to use Rails date helpers with a form. However, I only want to get the year. I got the form code below from simple_form gem https://github.com/plataformatec/simple_form. The discard_day and discard_month prevent the month and day select box from showing. However, when I submit the form, I'm getting this multiparameter error
2 error(s) on assignment of multiparameter attributes
"start(1i)"=>"2013",
"start(2i)"=>"4",
"start(3i)"=>"23",
"end(1i)"=>"2011",
"end(2i)"=>"4",
"end(3i)"=>"23",
It appears Rails is just substituting the current day and month since none was entered in the form.
I'm wondering if there's a way to prevent Rails from including the current day and month, or am I supposed to deal with it in the controller. Right now, the create action looks like this
def create
#employment = current_user.employments.build(params[:employment])
if #employment.save
....
If I can't stop Rails from including the current day and month, then what would I do in the controller?
<%= f.input :start, as: :date, start_year: Date.today.year - 90,
end_year: Date.today.year, discard_day: true, discard_month: true %>
<%= f.input :end, as: :date, end_year: Date.today.year - 90,
end_year: Date.today.year, discard_day: true, discard_month: true %>
What is your data type for the start and end columns? If you are just storing a year, you can set them to an integer type instead of a date, and use <%= f.input :start, collection: (Date.today.year - 90)..(Date.today.year) %>.

Rails - datetime_select - change order of time and date

I use datetime_select in my application and i want to use such order of time and date:
HH MM - DD MM YYYY
But i can't get how to do that. :order option allow only array that containing :day, :month and :year.
Is there any solution to solve that?
:order => [:hour, :minute, :day, :month, :year].
That should do it.

Resources