开发者

Rails - Trying to reconstruct and save a date before validation -> invalid date

开发者 https://www.devze.com 2023-02-15 23:56 出处:网络
I wrote a simple method which save a date in a db field (mydate:date) but it returns an \"invalid date\" error message.开发者_JS百科

I wrote a simple method which save a date in a db field (mydate:date) but it returns an "invalid date" error message.开发者_JS百科

note: I use simple_form

User.rb

attr_accessor:user_birthday_1i, :user_birthday_2i, :user_birthday_3i

before_validation :prepare_mydate

def prepare_mydate
  self.mydate = Date.new(self.user_birthday_1i.to_i, self.user_birthday_2i.to_i, self.user_birthday_3i.to_i)
end

form

<%= f.input :birthday, :as => :date, 
                       :start_year => Date.today.year - 100,
                       :end_year => Date.today.year,
                       :order => [:month, :day, :year],
                       :prompt => true %>

What's wrong with this? Thank you!


It looks like your form order is :month, :day, :year, whereas Date.new order is year, month, day.

I suggest you use the date_select helper.


before_save :prepare_mydate

def prepare_mydate
  self.mydate = Date.new(self.birthday.year.to_i, self.birthday.month.to_i, birthday_day.to_i)
end
0

精彩评论

暂无评论...
验证码 换一张
取 消