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' }
Related
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] }, {} %>
I am using drop down for selecting year in my form. How do I make my drop down defaults to current year. By using select_year method, we can use like
<%= select_year (Date.today, start_year: Date.today.year-1, end_year: Date.today.year+1) %>
Similarly how do I use it my form_for?
<%= i.select :year, (Date.today.year-1)..(Date.today.year+1), :selected => Date.today.year %>
Thanks in Advance.
You can use this
<%= i.select :year, (Date.today.year-1)..(Date.today.year+1), :selected => Time.current.year %>
I think it should be:
<% year = Date.today.year %>
<%= i.select :year, options_for_selection((year-1)..(year+1), year) %>
Hope this helps.
In my view I have:
<%= f.date_select :start %>
and I get the error message: can't convert Symbol into String
I know that it's related to it.date.order rule, but I see that rails-i18n include it:
https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/it.yml#L51
what's wrong here?
full back trace: https://gist.github.com/4007557
EDIT: running I18n.t 'date.order' in the console give me => [:day, :month, :year]. That it's correct... so why date_select doesn't works?
issue on GitHub repo: https://github.com/svenfuchs/rails-i18n/issues/273
I had a similar if not the same issue in the past. At the time I fixed it by using the following:
date:
order: [ !ruby/symbol day, !ruby/symbol month, !ruby/symbol year ]
As far as I understand the rails docs about date_select it wants to have a string.
If :start is the name of your I18n, you should do <%= f.date_select t(:start) %> as far as I remember.
You don't have to touch you form: it's a translation problem. You should add in your it.yml file the lines you will find here: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale.
If like in my case you are only working with years and don't want to add translations for every language with i18n just for a year selection, you can add :locale => 'en' just for that date like this:
<%= f.date_select :start, :start_year => 1940, :end_year => Date.today.year, :discard_day => true, :discard_month => true, :locale => 'en' %>
This is a translation problem: you have to add :order rule to your it.yml file or use this line in form.
<%= f.date_select(:start, :order => [:day, :month, :year]) %>
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)