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