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
Related
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.
Question: How do I split datetime into two separate form fields?
I have the following:
<%= f.label :Borrowed, "Check Out Date*" %></td>
<%= f.date_field :Borrowed, :id => "datepicker", min:Date.today, :ignore_time => true, :required => true %></td>
<%= f.label :Borrowed, "Check Out Time*" %>
<%= f.time_field :Borrowed, min:"9:00 AM", max: "4:30 PM", default:"10:00 AM", :ignore_date => true, :required => true%>
<td><%= f.label :Returned, "Check In Date" %>
<td><%= f.date_field :Returned, :id => "datepicker", min:Date.today, :ignore_time => true, :required => true %>
<td><%= f.label :Returned, "Check In Time*" %>
<td><%= f.time_field :Returned, min:"9:00 AM", max: "4:30 PM", default:"10:00 AM", :ignore_date => true, :required => true%>
I have two datetime fields in my database: Borrowed and Returned. I wanted to split up the date and the time so the user could pick a date from a calendar using a jQuery script. (This may be the problem...) What happens is when I fill out and submit the form the time saves correctly, but the date is the same on both Borrowed and Returned.
In the database it looks like this:
Returned: 2016-12-09 15:00:00 -0800
Borrowed: 2016-12-09 10:00:00 -0800
jQuery
$('#datepicker').datepicker({format: 'dd/mm/yyyy'});
Gems
gem 'bootstrap-timepicker-rails'
gem 'bootstrap-datepicker-rails'
You can't have two fields with the same name like:
<%= f.date_field :Borrowed, :id => "datepicker", min:Date.today, :ignore_time => true, :required => true %></td>
<%= f.time_field :Borrowed, min:"9:00 AM", max: "4:30 PM", default:"10:00 AM", :ignore_date => true, :required => true%>
Doing so will definitely give you the value for one field, and the value for the field with the same name will be overridden.
What you would like to can be accomplished using the following two ways:
1) Tinker with columns in your table
You may have two columns in your table to store two values: date, and time. Rails provides the following two datatypes to store things date and time.
add_column :table_name, :borrowed_date, :date # It will store day, month & year.
add_column :table_name, :borrowed_time, :time # It will store hours, minutes & seconds.
2) Use date_field_tag & time_field_tag
Instead of relying upon form_for to generate the values for us, in this scenario, you will have to take responsibility.
<%= date_field_tag :borrowed_d # other options %>
<%= time_field_tag :borrowed_t # other options %>
Now, Rails won't complain regarding the existence of columns: borrowed_d & borrowed_t, and at the same, it won't save the values for these columns either. So in your controller, now you manually need to take the values for borrowed_d, and borrowed_t, combine them into one, and save that thing against borrowed in the database.
Not only that, in case of editing an ActiveRecord object, you will have to gather the two values from borrowed, and show them in the form.
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 %>
The rails scaffolding code generates datetime_select like this:
<div class="field">
<%= f.label :time_end %><br />
<%= f.datetime_select :time_end, :order => [:month, :day] %>
</div>
However I only want to display month and day. The time helper has an option called :disable_minutes and :disable_seconds... however that doesn't work with a form_for helper.
Whats the best way to do this?
Well first off I would use date_select (api doc here) and then do :discard_year => true.
Example:
date_select("article", "written_on", :discard_year => true)
Use the :discard, e.g. :discard_year => true, :discard_hours => true
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.