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
Related
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.
I want to input only year field in my rails form. I tried select_year but it doesn't work for me. select_date also gave me problem because my form models is in association. Is there any alternative idea.
Ref:- select_year
# Generates a select field for years that defaults to the current year that
# is named 'birth' rather than 'year'
<%= f.select_year(Date.today, :field_name => 'birth') %>
but if you don't have any column then just use
<%= select_year(Date.today) %>
Ref this and try something like
<%= select_year Date.today, :start_year => Time.now.year, :end_year => Time.now.year - 95, :field_name => :grad_year, :prefix => :profile %>
Very simple question, but can't seem to work it out!! I'm creating a date of birth form, to ensure that a user can only sign up if they are over 18. I've got a date_select, and am fine setting the start year, but how do I set the end year so that it is 18 years ago? I'm going to use validators in the model as well, but don't want someone to be able to enter the wrong info in the first place.
I've looked all over, but can only find info on how to use time.ago with validators, which I've already sorted, not in the form itself.
Or, alternatively, which might be better - is there a way of having a date_select dropdown for the first two items (day & month), and a text-box for the year? Something along these lines:
<%= f.date_select :date_of_birth, :order => [:day, :month] %><%= f.text_field :date_of_birth.year, :length => 4 %>
This is what I have so far:
<%= f.label :date_of_birth %><br />
<%= f.date_select :date_of_birth, :order => [:day, :month, :year], :start_year => 1900, :end_year => %>
Thanks!!
Try
Time.now.year - 18
or
Date.today.year - 18
Found the answer on this page. Just changed it to -18 instead of +5:
<%= f.date_select :date_of_birth, :order => [:day, :month, :year], :start_year => 1900, :end_year => Time.now.year - 18 %>
You can also do:
18.years.ago.year
I am trying to create a db that has a year attribute but I am having some difficulty. I created the scaffold and tryied to modify the _form.html.erb with this code:
<%= f.date_select :year, :start_year=>2000, :end_year=>Time.now.year %>
Tried to run the rails server and gave me this error:
1 error(s) on assignment of multiparameter attributes
I realized that I only wanted the year and not the day or month. Is there a way to do that? I tried :discard_month=>true but that just hides it, but still storing it.
Thanks in advance
Do something like:
<%= f.date_select :year, :order => [:year], :start_year => 2000, :end_year => Time.now.year, :prompt => {:year => "Select year"} %>
Because your year field is an int and not a datetime:
<%= f.select :year, (2000..Time.now.year).to_a, :include_blank => {:year => "Select year"} %>
Another alternative is select_year
e.g.,
select_year(Date.current, start_year: 2015, end_year: Date.current.year)
I would like to use two picklists to control entry into a date field; I'm concerned with the month and year, not the day. So far, I have the following:
<tr>
<td>Date Range:</td>
<td>
<%= select_month(Date.today, :field_name=>"dateStarted") %>
<%= select_year(Date.today, :field_name=>"dateStarted") %>
to
<%= select_month(Date.today, :field_name=>"dateEnded") %>
<%= select_year(Date.today, :field_name=>"dateEnded") %>
</td>
</tr>
This will correctly set the month and year corresponding to today's date. Unfortunately, the select fields do not respond to values in the respective fields. What am I missing?
Moreover, I assume that I will need to combine these two fields in the controller's create and update actions to create an actual date that will be stored in the db, but I am unclear how to do so.
Help is appreciated.
Why not just use date_select (rather than select_month and select_year)
date_select("dateRange","dateStarted", {:default => Date.today, :discard_day => true})
date_select("dateRange","dateEnded", {:default => Date.today, :discard_day => true})
This will create parameters dateRange[dateStarted] and dateRange[dateEnded] with sub-selects for year and month.
One other clarification, to make it work seamlessly with the create and update action, you should replace "dateRange" with the actual model name. That way it will be properly embedded in the parameter hash
If you are using simple form in your rails application, then you can do something like:
= simple_form_for(resource) do |f|
// ...
= f.input :date_started, discard_day: true, end_year: Date.today.year, order: [:month, :year]
= f.input :date_ended, discard_day: true, end_year: Date.today.year, order: [:month, :year]