Rails - set date_select in form 18 years ago - ruby-on-rails

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

Related

Undefined method `merge' for 2000..2040:Range

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

Rails date/year field

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 %>

Create a Year Dropdown Box FormBuilder In Ruby On Rails

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)

Rails 3 date_select for year only

I have a form in my Rails 3 app where I want to create a select tag for just the year on a method :grad_year. I have everything working - and storing - properly using date_select and adding :discard_month and :discard_day. However when I render #profile.grad_year I get the month and day values. So I'm wondering how to store and render only the year for #profile.grad_year?
Here is the form:
<%= f.date_select :grad_year, {:start_year => Time.now.year, :end_year => Time.now.year - 95, :discard_day => true, :discard_month => true}, :selected => #profile.grad_year %>
In my migration:
t.date :grad_year
Rails has a select_year helper:
http://apidock.com/rails/ActionView/Helpers/DateHelper/select_year
So your code should look like:
f.select_year(Date.today, :start_year => Time.now.year, :end_year => Time.now.year - 95, :field_name => 'grad_year')
Assembling all of the above from #alex_peattie's answer, I arrived at the following:
<%= select_year Date.today, :start_year => Time.now.year, :end_year => Time.now.year - 95, :field_name => :grad_year, :prefix => :profile %>
As with the OP's question, my case was done within a form_for block, so f.select_year threw an exception. But if you just use the documented :field_name option, the tag will have the id date_grad_year and name date[grad_year] which are not what Rails expects. Using the (documented only at the very top of the API) :prefix option changes date to profile.
So this is better than the ##%$^*& html_options hash, which, despite using rails for 5 years now, I cannot seem to get right without five tries :-).
Oh Rails, how I love you, yet at the same time am sure glad Stack Overflow is around to help us all understand your delightful idiosyncrasies!
This select_year function is totally screwy.
Here is finally what works:
<%= form_for(#user) do |f| %>
<%= select_year current_user.birth_year, { :prompt => "Year",
:start_year => Time.zone.now.year - 13,
:end_year => Time.zone.now.year - 80,
:field_name => :birth_year,
:prefix => :user },
class:"form-control" %>
<% ... %>
it aught to be like this in rails:
<%= f.select_year :birth_year, { :prompt => "Year",
:start_year => Time.zone.now.year - 13,
:end_year => Time.zone.now.year - 80},
class:"form-control" %>

Rails date select options?

I have a date_select field in my rails application as follows:
<%= f.date_select :dateinstructed %>
I would like to re-order the drop down lists show they output as:
DD/MM/YYYY
According to what I have read you can use the :order option, but I am unsure how to actually use this option:
<%= f.date_select :dateinstructed, :order = {:day, :month, :year} %>
Obviously this isn't right, but what am I supposed to put in place of the:
:day, :month, :year
Any help would be appreciated!
Thanks,
Danny
I think it should be:
<%= f.date_select :dateinstructed, :order => [:day, :month, :year] %>
Hopefully it helps.
How about this:
prompt: { day: 'Select day', month: 'Select month', year: 'Select year' }

Resources