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
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.
I have been looking for a solution for this problem for hours. There are plenty of recommendations even here on stackoverflow and I tried all of them but none of them work in my case and I don't know why.
I have the current version of RoR [4.1.5] running on my server and I have a form in which the user has to select a "time from" and a "time to" value.
<p>
<%= f.label :Time %><br>
<%= f.time_select :timefrom, {minute_step: 60} %> to <%= f.time_select :timeto, {minute_step: 60} %>
</p>
I get the default values from the database. In order to fetch them, I just collect the last time value which has been entered. Which usually is a timeframe of 1 hour:
<% changetimeto = (Production.select("timeto").last.timeto).strftime("%H:%M") %>
<% changetimefrom = (Production.select("timeto").last.timeto + 3600).strftime("%H:%M") %>
So I tried:
<%= f.time_select :timefrom, {minute_step: 60}, :selected => changetimeto %> to <%= f.time_select :timeto, {minute_step: 60}, :selected => changetimefrom %>
or :value => or :default =>
but nothing seems to work, it doesn't even give me an error. I checked the variables in changetimeto and changetimefor and they have the right value, its like "14:00" and "15:00". But as I said, it doesn't do anything.
I also tried to address hours and minutes separately with :default => {hours: '14', minutes: '00'}, etc. but that also doesn't work, not even causing an error. Just nothing. It still defaults to the actual time.
Does anyone have a recommendation or hint for me?
Defaults in time_select (for those who've not been following the comment thread) work like this:
<%= time_select :name, 'method', {options}, {:default => {:hour => '10', :minute => '30'}} %>
If we want the time select to default to 10:30.
f.time_select :start, default: Time.current.change(hour: 9, min: 0)
or
f.time_select :start, default: {hour: 9, min: 0}
ref: https://github.com/rails/rails/blob/94b5cd3a20edadd6f6b8cf0bdf1a4d4919df86cb/actionview/lib/action_view/helpers/tags/date_select.rb#L44
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]) %>
I am using Rails 3.1 and first time using datetime_select. I want to specify the minute select box to have 15 minute intervals rather than 1 minute intervals:
<%= f.datetime_select :start_datetime, :ampm => true, :start_minute => [0,15,30,45] %>
But this doesn't seem to work. The :start_datetime is what the property is called in the Event model. Is it possible that this is a reserved word that is causing problems?
Try passing :minute_step => 15
<%= f.datetime_select :start_datetime, :ampm => true, :minute_step => 15 %>
From what I can tell by looking in the Rails source code, it looks like all the methods are there. I just can't figure out how to call time_select with the right options to enable 12 hour and add a AM/PM select element. What options do I need pass in order to create this?
Here are a few things that I've tried:
<%= form_for(#post) do |f| %>
<div class="field">
<%= f.label :created_at %><br />
<%= f.time_select :created_at, :ampm => true #didnt do anything %>
<%= time_select('post', 'created_at', { :ampm => true }) #also didnt do anything %>
</div>
<% end %>
time_select("post", "written_on", {:ampm => true})
Update
You need to update your Rails version. The ampm selector was added in this commit
commit a869382a9fa241f72a7a73d4a9738531c4c37ba5 Author: Aditya Sanghi
Date: Fri Apr 29 01:49:45 2011 +0530
Allow AM/PM in datetime selectors
which is not in 3.0.x. Sorry.