参数超出范围Rails 4和bootstrap3-datetimepicker-rails [英] Argument out of range Rails 4 and bootstrap3-datetimepicker-rails

查看:175
本文介绍了参数超出范围Rails 4和bootstrap3-datetimepicker-rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 bootstrap3-datetimepicker-rails 宝石,让用户存储我的应用程序(DateTime属性)中的 WorkOrder scheduled_date ,但是我得到一个参数超出范围错误,当用户提交表单创建 WorkOrder 时,日期与所选择的日期非常不同用户弹出编辑屏幕。有趣的是,它曾经工作,但我不知道什么代码可能改变以打破它。

I'm using the bootstrap3-datetimepicker-rails gem to let users store the scheduled_date of a WorkOrder in my application (a 'DateTime' property), but I'm getting an argument out of range error when the user submits the form to create the WorkOrder, and the date is very different than what was selected whenever the user pops open the edit screen. The funny thing is that it used to work, but I don't know what code could have changed to break it.

我添加了所需的依赖关系和在我的CSS / JS文件中包含正确的指令:

I added the required dependencies & included the proper directives in my CSS/JS files:

gem 'momentjs-rails', '~> 2.5.0'
gem 'bootstrap3-datetimepicker-rails', '~> 3.0.0'

据我所知,我的代码与文档中的示例相同。以下是我的表单片段:

As far as I can tell, my code looks identical to the example on the documentation. Here's the snippet of my form:

  <div class="row">
    <div class="col-md-4">
      <div class="form-group">
        <%= f.label :scheduled_date %>
        <div class='input-group date'>
          <span class="input-group-addon"><span class="fa fa-calendar"></span></span>
          <%= f.text_field :scheduled_date,  class: 'form-control input-lg', id: "service_date_picker" %>
        </div>
      </div>
    </div>
  </div>


  <script type="text/javascript">
      $(function () {
        $('#service_date_picker').datetimepicker({
          icons: {
            time: "fa fa-clock-o",
            date: "fa fa-calendar",
            up: "fa fa-arrow-up",
            down: "fa fa-arrow-down"
          }
        });
      });
  </script>

编辑(忘记添加控制器代码 - 超级简单):

EDIT (forgot to add controller code - super simple):

work_orders_controller.rb

def create
  @work_order = WorkOrder.new(work_order_params)

  ... [it errors on the line above]
end

我的参数白名单:

def work_order_params
  params.require(:work_order).permit(:caller_name, :brief_job_details, :contract_price, :quoted_by, :job_description, :scheduled_date, :time_materials_details, :sheduled_time, :contact_name, :payment_method, :notes, :customer_id, :job_site_id)
end

还有一个给参数超出范围的参数范例错误当我保存表单:

And an example of the parameters that give the argument out of range error when I save the form:

work_orders_attributes"=>{"0"=>{"caller_name"=>"Justin", "time_materials_details"=>"", "contract_price"=>"", "quoted_by"=>"", "payment_method"=>"", "job_description"=>"", "scheduled_date"=>"05/23/2014 1:28 PM", "notes"=>""}}

与我的 customers_controller.rb 相同,其中我有一个嵌套的表单 WorkOrder 在新的客户表单内(该表单上的其他内容完美):

Same as my customers_controller.rb where I have a nested form for a WorkOrder inside of the new customer form (everything else on that form works perfectly):

def create
  @customer = Customer.new(customer_params)

  ... [errors above]
end

如果日期和时间在现在24小时内,日期和时间可以正确保存,但即使正确保存,日期在文本字段中显示为非常奇怪且未格式化,当我去编辑 WorkOrder 。例如:

The date and time saves correctly if it is within 24 hours of now, but even when it saves correctly, the date displays completely weird and unformatted in the text field when I go to edit that WorkOrder. For instance:

当我从日期选择器中选择日期时: 05/06/2014 10:28 AM

When I select the date from the date picker: 05/06/2014 10:28 AM

当我编辑 WorkOrder 时: 2014-06-05 10:28:00.000000

任何想法?任何偏好我是否应该使该字段为 Date Time DateTime (当前)?现在我们正在中区时区工作,但并不总是这样。

Any ideas? Any preference for whether I should make that field a Date, Time, or DateTime (currently)? Right now, we're working in Central time zone, but that won't always be the case.

提前感谢指出我正确的方向! p>

Thanks in advance for pointing me in the right direction!

推荐答案

@Uri Agassi是对的,这是你传递给创建 WorkOrder 这是最相关的。

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

尝试用自定义日期格式化选项 bootstrap-datepicker 提供给您。如果您可以获得看起来不错的格式,并且很容易在后端解析,那么您需要在该模型进行验证之前拦截该参数。

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.

不管客户端的日期格式如何,您仍然需要将其解析成可用的 Date DateTime 控制器中的对象服务器端。 退房 DateTime#strptime 。您还可以在日期上调用 #strptime

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.

未知的,但是想法是:

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

这篇关于参数超出范围Rails 4和bootstrap3-datetimepicker-rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆