0

好的,我在人、用户和员工之间建立了一种关系,即所有员工都是用户,所有用户都是人。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 对象中?

4

1 回答 1

0

我需要的是validates_associated这样我会有:

员工.rb

validates_associated :user

用户.rb

validates_associated :person
于 2010-03-26T10:51:54.470 回答