Ruby on rails 单独的模型日期字段和验证轨道
我将此宝石用于我的表单: 我有3个输入字段,分别是日、月、年:Ruby on rails 单独的模型日期字段和验证轨道,ruby-on-rails,ruby,simple-form,Ruby On Rails,Ruby,Simple Form,我将此宝石用于我的表单: 我有3个输入字段,分别是日、月、年: = f.input :dob_day, label_html: {style: 'display:none'}, placeholder: 'DD' = f.input :dob_month, label_html: {style: 'display:none'}, placeholder: 'MM' = f.input :dob_year, label_html: {style: 'display:none'}, placeho
= f.input :dob_day, label_html: {style: 'display:none'}, placeholder: 'DD'
= f.input :dob_month, label_html: {style: 'display:none'}, placeholder: 'MM'
= f.input :dob_year, label_html: {style: 'display:none'}, placeholder: 'YYYY'
在我的模型(重要一点)中,我试图为出生日期建立一个验证:
class User < ActiveRecord::Base
attr_writer :dob_day, :dob_month, :dob_year
def dob_day
@dob_day
end
def dob_month
@dob_month
end
def dob_year
@dob_year
end
user.validates :dob_day, presence: true
user.validates :dob_month, presence: true
user.validates :dob_year, presence: true
user.validates :dob, presence: true,
date: { before: Proc.new { Time.now - 18.years }, message: ": You must be over 18 to join" }
class用户
现在我得到了一个错误:
无法批量分配受保护的属性:月日、月日、年
我想在dob字段中注入dob_day、dob_month、dob_year的值。如果可能的话,我会在模型中做,而不是在控制器中
我这样做不对吗?这应该相当简单,但事实证明它不是可以删除的
as: :date, start_year: Date.today.year - 90, end_year: Date.today.year - 18, order: [:month, :year, :day]
然后,您可以使用显示日历来选择一个日期将此
删除为::date,start\u year:date.today.year-90,end\u year:date.today.year-18,顺序:[:月,:年,:日]
为了防止批量分配错误,您需要添加:attr\u accessible:dob\u day,:dob\u month,:dob\u year