0

Rails 3.x には、int フィールド「my_int_date」を持つモデル クラスがあります。date_select フォーム要素として int フィールドにフォームを入力したいと思います。

問題は、フォームが date_select 値を読み取るときに、それをマルチパラメータ値として送り返し、エラーが発生することです。

1 error(s) on assignment of multiparameter attributes

モデルに Date として保存しておき、DB にダンプするときに int に変換できる方法はありますか?

4

2 に答える 2

0

これが私がそれを解決した方法です、

私の ActiveRecord モデルでは、フィールドを追加しました

attr_accessible :my_int_date_time

columns_hash["my_int_date_time"] = ActiveRecord::ConnectionAdapters::Column.new("my_int_date_time", nil, "DateTime")

def my_int_date_time
  return TimeHelper.datetime_from_integer(self.my_int_date)
end

def my_int_date_time= val
    self.my_int_date = TimeHelper.datetime_to_integer(val)
end

私のフォームでは、日時フィールドを my_int_date にリンクするのではなく、フィールド my_int_date_time にリンクしました

于 2012-08-16T21:59:44.337 に答える
0

Ruby は to_i を使用して Time オブジェクトを秒に変換できます

Time.now.to_i # Returns epoc time (s)

文字列の場合は、メソッド to_time を使用できます

'2012-06-01'.to_time.to_i # string to Time object then to epoc time (s)
于 2012-08-06T22:01:09.473 に答える