2

Rails モデルの Date フィールドを datepicker としてレンダリングしようとしています。

モデルは次のようになります。

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に伝えるにはどうすればよいですか?

4

3 に答える 3

2

私はパーティーに少し遅れましたが...

Mongoid は、日付としてフィードする文字列の形式にかなりこだわっています。私が理解しているように、dd-mm-yyyy だけで十分です。幸いなことに、jQuery UI の日付ピッカーには、出力をフォーマットするオプションがあります。Mongoid が望む形式は次のようになります。

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

日付ピッカーのオプションと形式の詳細については、次を参照してください。

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

乾杯!

于 2013-03-01T06:08:34.313 に答える
1

理解した。もう 1 つのフィールド date_string を attr_accessor として追加しました。このフィールドはデータベースには保存されませんが、フォームに表示され、内部の日付フィールドに変換するために使用できます。モデルは次のように変更されます。

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();
    });

これは正しく機能し、フィールドがデータベースに正しく設定されることを確認しました。

于 2013-01-25T17:19:32.563 に答える
0

問題は、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
      });
    })
于 2014-02-20T07:49:51.790 に答える