Rails - 尝试在验证之前重建并保存日期 ->无效日期
我编写了一个简单的方法,它将日期保存在数据库字段(mydate:date)中,但它返回“无效日期”错误消息。
注意:我使用 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
表单,
<%= f.input :birthday, :as => :date,
:start_year => Date.today.year - 100,
:end_year => Date.today.year,
:order => [:month, :day, :year],
:prompt => true %>
这有什么问题吗? 谢谢你!
I wrote a simple method which save a date in a db field (mydate:date) but it returns an "invalid date" error message.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您的表单顺序是:月、:日、:年,而Date.new
顺序是年、月、日。我建议您使用
date_select
帮助器。It looks like your form order is :month, :day, :year, whereasDate.new
order is year, month, day.I suggest you use the
date_select
helper.