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

后端 未结 4 744
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 08:29

I\'m using the bootstrap3-datetimepicker-rails gem to let users store the scheduled_date of a WorkOrder in my application (a \'DateTime\' property), bu

相关标签:
4条回答
  • 2021-02-07 08:56

    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'});
    });
    
    0 讨论(0)
  • 2021-02-07 09:07

    @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
    
    0 讨论(0)
  • 2021-02-07 09:10

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

    $(function() {
      $('.datepicker').datepicker({format: 'dd-mm-yyyy'});
    });
    
    0 讨论(0)
  • 2021-02-07 09:20

    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
    
    0 讨论(0)
提交回复
热议问题