好的,我在人、用户和员工之间建立了一种关系,即所有员工都是用户,所有用户都是人。Person
是一个User
派生自它的抽象类Employee
。
现在......我有一个EmployeesController
类,create
方法如下所示:
def create
@employee = Employee.new(params[:employee])
@employee.user = User.new(params[:user])
@employee.user.person = Person.new(params[:person])
respond_to do |format|
if @employee.save
flash[:notice] = 'Employee was successfully created.'
format.html { redirect_to(@employee) }
format.xml { render :xml => @employee, :status => :created, :location => @employee }
else
format.html { render :action => "new" }
format.xml { render :xml => @employee.errors, :status => :unprocessable_entity }
end
end
end
如您所见,当我使用该:polymorphic => true
子句时,您访问超类的方式是执行类似@derived_class_variable.super_class_variable.super_super_etc
. Person
班级有一个,当validates_presence_of :first_name
它满意时,在我的表格上,一切都很好。但是,如果我省略了名字,它不会阻止员工得救。发生的情况是员工记录已保存,但人员记录未保存(因为验证)。
如何让验证错误显示在 flash 对象中?