date_select to current and past month select only in rails - ruby-on-rails

in form i used date_select but by default it adds all month of year. i am able to restrict year to current and past year but i am not able to restrict month. i want when user select 2020 they won't see September in there drop-down.
here is my current code-
<div class="field medium-4 columns">
<%= form.label :attendance_month %>
<%= form.date_select :attendance_month, { :discard_day => true, :discard_month => false, :discard_year => false, :end_year => Time.now.year },:class => 'month-and-year' %>
</div>

Using date_select you have only the option of restricting the year but not the month and day.
Either you can add model validation on date and raise error. Or you can use jQuery Date Picker plugin and set minDate and maxDate options to restrict the date range.

Related

how to select current and past month-year in rails?

i am using date_select to select month and year but by default this is able to select future month and year. i want to restrict selection of future month and year. how can do this in rails?
here is my current code-
<div class="field medium-4 columns">
<%= form.label :attendance_month %>
<%= form.date_select :attendance_month, { :discard_day => true, :discard_month => false, :discard_year => false },:class => 'month-and-year' %>
</div>
With :date_select you are unable to specify an 'end_month', instead I would create a helper method:
# app/helpers/some_helper.rb
module SomeHelper
def month_range
( Date.today.at_beginning_of_year..Date.today ).each_with_object( [] ) do | date , month_array |
month_array << date.strftime( "%B %Y" )
end.uniq
end
end
and then I would use month_range as part of a select_tag:
<%= form.select :attendance_month, month_range, :class => 'month-and-year' %>
You can restrict selection of dates by passing in end_year.
:end_year => Time.now.year
Further reading:
Action View Date Helpers:date_select
:end_year - Set the end year for the year select. Default is Date.today.year + 5 if you are creating new record. While editing existing record, :end_year defaults to the current selected year plus 5.

Issue for increase the date choices for datepicker in f.input

I want to open the range of dates in my date picker. It's plugged in a form input <%= f.input :birthday_date>.
From now it's possible for users to choose between 2014 -> 2024 I want user could pick a date between 2005 and Time.now.year ?
You could use a date_select and specify the :start_year and :end_year:
<%= f.date_select :birthday_date, start_year: Date.new(2005, 01, 01).year, end_year: Date.today.year %>
See: date_select()

Split datetime into two separate form fields (RAILS)

Question: How do I split datetime into two separate form fields?
I have the following:
<%= f.label :Borrowed, "Check Out Date*" %></td>
<%= f.date_field :Borrowed, :id => "datepicker", min:Date.today, :ignore_time => true, :required => true %></td>
<%= f.label :Borrowed, "Check Out Time*" %>
<%= f.time_field :Borrowed, min:"9:00 AM", max: "4:30 PM", default:"10:00 AM", :ignore_date => true, :required => true%>
<td><%= f.label :Returned, "Check In Date" %>
<td><%= f.date_field :Returned, :id => "datepicker", min:Date.today, :ignore_time => true, :required => true %>
<td><%= f.label :Returned, "Check In Time*" %>
<td><%= f.time_field :Returned, min:"9:00 AM", max: "4:30 PM", default:"10:00 AM", :ignore_date => true, :required => true%>
I have two datetime fields in my database: Borrowed and Returned. I wanted to split up the date and the time so the user could pick a date from a calendar using a jQuery script. (This may be the problem...) What happens is when I fill out and submit the form the time saves correctly, but the date is the same on both Borrowed and Returned.
In the database it looks like this:
Returned: 2016-12-09 15:00:00 -0800
Borrowed: 2016-12-09 10:00:00 -0800
jQuery
$('#datepicker').datepicker({format: 'dd/mm/yyyy'});
Gems
gem 'bootstrap-timepicker-rails'
gem 'bootstrap-datepicker-rails'
You can't have two fields with the same name like:
<%= f.date_field :Borrowed, :id => "datepicker", min:Date.today, :ignore_time => true, :required => true %></td>
<%= f.time_field :Borrowed, min:"9:00 AM", max: "4:30 PM", default:"10:00 AM", :ignore_date => true, :required => true%>
Doing so will definitely give you the value for one field, and the value for the field with the same name will be overridden.
What you would like to can be accomplished using the following two ways:
1) Tinker with columns in your table
You may have two columns in your table to store two values: date, and time. Rails provides the following two datatypes to store things date and time.
add_column :table_name, :borrowed_date, :date # It will store day, month & year.
add_column :table_name, :borrowed_time, :time # It will store hours, minutes & seconds.
2) Use date_field_tag & time_field_tag
Instead of relying upon form_for to generate the values for us, in this scenario, you will have to take responsibility.
<%= date_field_tag :borrowed_d # other options %>
<%= time_field_tag :borrowed_t # other options %>
Now, Rails won't complain regarding the existence of columns: borrowed_d & borrowed_t, and at the same, it won't save the values for these columns either. So in your controller, now you manually need to take the values for borrowed_d, and borrowed_t, combine them into one, and save that thing against borrowed in the database.
Not only that, in case of editing an ActiveRecord object, you will have to gather the two values from borrowed, and show them in the form.

Making dates go back passed 2011 DateTime Generator on Ruby on Rails Scaffold

I am making a new site and I cannot get dates in a generated scaffold to go back passed 2011. The months , days, and time work perfectly. However, I cannot moved the year past back 2001. I am making an Astrology Porn site with a database of compatible female pornstars with an inputted zodiac sign. I hope you can help me fix the date issues I am trying to add Kianna Dior to the zodiac sign and she was born in 1969, so it will not work with rails 5 default generator for any dateTime columns that are generated via scaffold.
You can try to change the date input options:
date_select :date, :start_year => Date.current.year, :end_year => 1920
date_select api documentation
This worked in _form.html.erb
<div class="field">
<%= f.label :datez %>
<%= f.date_select :datez, :start_year => Date.current.year, :end_year => 1920 %>
</div>
Thanks for the input

end of month Ruby

My Ruby app is showing the beginning of the month (day 1) when processing:
start_month => Date.today.month
I would like the end of the month, so I changed it to:
start_month => Date.today.end_of_month
but it is showing the beginning of the month. Would you know why?
Based on the comments, I rewrote as follows, yet it still does not work. I am sure I am misspelling something
<div class="field">
<%= f.label :delivered_at, "Estimated delivery" %><br />
<%= f.date_select(:delivered_at.end_of_month, :start_year => Date.today.year, :start_month => Date.today.end_of_month,:prompt => { :month => 'Choose month', :year => 'Choose year'}, :discard_day => true) %>
</div>
Given the code for the views you've shown, I'm guessing your problem is in your controller code. If you look at the documentation for f.date_select, discarding the day causes it to default to the first day of the month. Unless you're doing something with delivered_at in your controller, it's always going to be the first day of the month, so you need to change it to delivered_at.end_of_month before saving the model.
Date.today.to_date.end_of_month
output :
Sat, 30 Nov 2013
AND
Date.today.to_date.end_of_month.month
output:
11

Resources