Rails - Trying to reconstruct and save a date before validation -> invalid date
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
精彩评论