【问题标题】:best_in_place date formatbest_in_place 日期格式
【发布时间】:2014-12-20 00:50:37
【问题描述】:

我知道我的问题是什么,我只是不知道去哪里寻求解决方案。我正在使用带有日期的 best_in_place。我弹出了日期选择器,我可以选择一个日期。我认为,我的问题出在我的控制器上。如果我选择一个具有相同月份和日期的日期,例如 12 月 12 日或 2 月 2 日,它会起作用。但是,如果我选择 12 月 11 日或 2 月 3 日,则日期将保存为 11 月 12 日或 3 月 2 日。

我的控制器代码是

def update
  respond_to do |format|
    if @order.update(order_params)
      puts "Updating! #{order_params}"
      format.html { redirect_to @order, notice: 'Order was successfully updated.' }
      format.json { respond_with_bip(@order) }
    else
      format.html { render :edit }
      format.json { render json: @order.errors, status: :unprocessable_entity }
    end
  end
end

如果我选择 12 月 19 日,我的 "puts" 输出 {"ordered_on"=>"12/19/2014"} 并没有保存在数据库中。我使用 postgresql 作为后端,我猜问题是数据库需要字符串 dd/mm/yy 而不是 mm/dd/yy,这就是为什么 2 月 3 日被保存为 3 月 2 日的原因。

解决这个问题的最佳位置是什么?

【问题讨论】:

    标签: ruby-on-rails-4 postgresql-9.3 best-in-place


    【解决方案1】:

    Rails 只接受特定格式的日期。在您的 javascript 中,您必须为 datepicker 字段设置默认格式。

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

    【讨论】:

      猜你喜欢
      • 2012-08-03
      • 2015-09-11
      • 2012-11-15
      • 2010-11-03
      • 2017-12-17
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多