Argument out of range Rails 4 and bootstrap3-datetimepicker-rails

冷暖自知 提交于 2019-12-03 05:13:25

@Uri Agassi is right: it's the values that you're passing to create the WorkOrder that are the most relevant here.

Try putzing around with the custom date formatting options provided to you by bootstrap-datepicker. If you can get a format that looks good and is easy to parse on the backend, then you'll want to intercept that parameter before it goes to the model for validation.

Regardless of what you'll go with for date formatting on the client-side, you'll still want to parse it into a useable Date or DateTime object server-side in your controller. Check out DateTime#strptime. You can also call #strptime on Date.

Untested, but the idea is there:

def create
  order_params = work_order_params
  order_params[:scheduled_date] = Date.strptime(order_params[:scheduled_date],
                                                '%m/%d/%Y %I:%M %p')
  @work_order = WorkOrder.new(order_params)
end

You can remove the modifications from the Controller all together by updating the values in the model. I feel this is a much cleaner (and reusable) approach.

class Work < ActiveRecord::Base

  def scheduled_date=(date)
     begin
       parsed = Date.strptime(date,'%m/%d/%Y %I:%M %p')
       super parsed
     rescue
       date         
     end
  end

end

It's me helped:
To file application.js writing to end file:

$(function() {
  $('.datepicker').datepicker({format: 'dd-mm-yyyy'});
});

This plugin uses moment.js. Basically you need to choose the time format you would like.

example

$(function () {
 $('#datetimepicker').datetimepicker({format: 'MMMM Do YYYY, h:mm'});
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!