【问题标题】:Using a jquery datepicker with a Rails MongoId Date field results in a "argument out of range" error使用带有 Rails MongoId 日期字段的 jquery datepicker 会导致“参数超出范围”错误
【发布时间】:2013-01-25 16:22:22
【问题描述】:

我正在尝试将我的 rails 模型的日期字段呈现为日期选择器。

模型如下:

class Appointment
  include Mongoid::Document
  field :date, type: Date  
end

_form.html.haml 视图如下:

= form_for @appointment, :url => {:action => :create} do |f| 
  = f.text_field(:date, {:class => 'datepicker'})
  %button{:type => 'submit'} Book appointment

:javascript
    jQuery(document).ready(function($) {
      $('.datepicker').datepicker();
    });

控制器动作如下:

class AppointmentsController < ApplicationController
  def create
    @appointment = Appointment.new(params[:appointment])

    # rest left out for demo purposes
  end
end

当“new”获取时,调用发生错误:

ArgumentError in AppointmentsController#create

argument out of range

我知道该值以 MM/DD/YYYY 的形式发布,即 03/11/2013

如何告诉 Rails 如何正确序列化该字段?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongoid


    【解决方案1】:

    我参加聚会有点晚了,但是...

    Mongoid 非常注重作为日期输入的字符串的格式。据我了解,只有 dd-mm-yyyy 可以。幸运的是,jQuery UI 日期选择器为您提供了格式化其输出的选项。 Mongoid 想要的格式如下所示:

    $('.datepicker').datepicker({ dateFormat: 'dd-mm-yy' });
    

    更多关于日期选择器选项和格式的信息在这里:

    http://api.jqueryui.com/datepicker/#option-dateFormat

    干杯!

    【讨论】:

      【解决方案2】:

      想通了。我添加了另一个字段 date_string 作为 attr_accessor,它不会存储在 db 中但可以显示在表单中,并可用于转换为内部日期字段。模型改为:

      class Appointment
        # extend these two to get accesss to drop down options
        include Mongoid::Document
        before_validation :parse_date_if_not_null
      
        #person info
        field :date, type: Date
      
        attr_protected :date
        attr_accessor :date_string
      
        def parse_date_if_not_null
          unless self.date_string.nil? || self.date_string == ''
            self.date = Date.strptime self.date_string, '%m/%d/%Y'
          end
        end
      end
      

      在视图中,使用了date_string字段:

      = form_for @appointment, :url => {:action => :create} do |f| 
        = f.text_field(:date_field, {:class => 'datepicker'})
        %button{:type => 'submit'} Book appointment
      
      :javascript
          jQuery(document).ready(function($) {
            $('.datepicker').datepicker();
          });
      

      这可以正常工作,并且我已验证该字段已正确设置在数据库中。

      【讨论】:

        【解决方案3】:

        问题是 rails/activerecord 期望日期为 ISO 格式“yyyy-mm-dd”。但这不是一种用户友好的格式

        我认为最简单的解决方案是在 datepicker 上使用 alt 属性 - 基本上,显示日期格式但提交另一种日期格式:

          = f.text_field :your_date, id: "your_date", class: "hidden" // This is hidden
          // This will show up
          = text_field_tag "date[something_we_will_not_use]", f.object.your_date.strftime("%m/%d/%Y"),
            class: "datepicker", 'data-alt-field' => '#your_date'
        

        日期选择器初始化

        $('.datepicker').each(function() {
        
              var alt_field = $(this).attr('data-alt-field');
        
              $this.datepicker({
                dateFormat: "mm/dd/yy",
                prevText: '<i class="fa fa-chevron-left"></i>',
                nextText: '<i class="fa fa-chevron-right"></i>',
                altField: alt_field, // This is the tag where the date will be filled
                altFormat: "yy/mm/dd" // This is how the date will be posted to your controller
              });
            })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多