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.
Related
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.
For some odd reason, the year is still showing as the default value for the select_year input that rails offers. I cannot get include_blank: "Select Year" to populate the form input. How can I force the select_year/select_month input to show the include_blank text instead of the current year/current day value? fyi: prompt also doesn't work for the input
_calendar_form.html.erb
<%= form_tag calendar_path(#calendar), method: :get do %>
<%= select_year(Date.today, {:prompt => "Select Year", :start_year => DateTime.now.year, :end_year => DateTime.now.year - 8, prefix: 'select'}, {:field_name => 'year', :id => 'start-year', class: 'ui dropdown', include_blank: 'Select Year'}) %>
<% end %>
It's because your first argument is the value it should set, as in Date.today
You can set that to nil to get it to work. You can also just skip the include_blank since you also have prompt
<%= select_year nil, {prompt: "Select Year", start_year: DateTime.now.year, end_year: 8.years.ago.year, prefix: 'select'}, {field_name: 'year', id: 'start-year', class: 'ui dropdown', include_blank: 'Select Year'} %>
Usually you will set this in a controller variable
def new
#date = nil
end
def create
#date = some_params[:year] # depending on use-case obviously
# if you need to re-render the form the value is retained
end
= select_year #date ......
I have rails form with date_select field in rails application. by default it shows 10 years from now in drop down list, but I want to access particular year in drop down. How can I do that in rails application?
Here is what I tried and I got error:
undefined method `merge' for 2000..2040:Range
<div class="field columns large-3">
<%= form.label :planned_start_date, :class=>"required" %>
<%= form.date_select :planned_start_date, Date.today.year-20 .. Date.today.year+20, :include_blank => true, order: [:day, :month, :year], class: 'select-date' %>
</div>
Accordingly to documentation date_select accepts these options:
:start_year - Set the start year for the year select. Default is Date.today.year - 5 if you are creating new record. While editing existing record, :start_year defaults to the current selected year minus 5.
: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.
So just replace your range to proper options hash as:
<%= form.date_select :planned_start_date, start_year: Date.today.year - 20, end_year: Date.today.year + 20, :include_blank => true, order: [:day, :month, :year], class: 'select-date' %>
And this should work
I'm using SimpleForm but I've read its date input takes the same options as Rails's built in helper.
I want to display a date picker that allows the user to select a date within a two month range from now.
I know that I can set a start and end year. But how do I limit the range to a number of months?
Something like:
<%= simple_form_for #order, url: orders_path do |form| %>
<%= form.input :delivery_date,
order: [:day, :month, :year],
include_blank: true,
start_month: Time.now.month,
end_month: Time.now.month + 2
%>
<%= form.button :submit %>
<% end %>
I'm pretty sure simple_form delegates this to the Rails date_select method in which case you have to use start_year and end_year.
The basic date picker in Rails is pretty simple and I'm not sure it does what you're after out of the box.
= form.input :delivery_date,
order: [:day, :month, :year],
include_blank: true,
start_year: 1.year.ago,
end_year: Time.now.year
`https://apidock.com/rails/ActionView/Helpers/DateHelper/date_select
You can do something like this:
<%= form.select :delivery_date, ((Date.today)...(Date.today + 2.months)).map{ |date| [date.strftime("%d %B %Y"), date] }, {} %>
Here is the error I am receiving.
1 error(s) on assignment of multiparameter attributes [error on assignment [3, 3] to backup (Missing Parameter - backup(1))]
Here is my code.
<% #days = [] %>
<% 6.downto(0).each do |number| %>
<% #days << number.days.ago.to_date %>
<% end %>
<%= f.select :backup, #days.collect { |d| [ Date::DAYNAMES[d.wday], d ] } %><br>
<%= f.time_select :backup, :ignore_date => true %><br>
Currently in the f.select field, I can choose a day name from the last 6 days and the value would be the actual date that is inserted into the DB. I don't see a way to select the day names as I currently am doing if I were to use f.datetime_select.
Can anyone please explain why this error is coming up?
Why don't you use datetime_select straight away?
<%= f.datetime_select(:backup, :ampm => true, :start_year => 2000, :end_year => 2025, :order => [:day, :month, :year], :default => { :hour => 00, :minute => 00 }) %>
You could also do
<%= f.datetime_select(:backup, :ampm => true, default: 6.days.ago) %>
If you want the start to be 6 days ago.
Your DB should support the datetime format as well.