0

OK、私は人、ユーザー、従業員の間に、すべての従業員がユーザーであり、すべてのユーザーが人であるという関係があります。は、そこから派生し、そこから派生するPerson抽象クラスです。UserEmployee

今...私は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_etcPersonクラスにはがあり、それが満たされたvalidates_presence_of :first_nameとき、私のフォームでは、すべてがOKです。ただし、名を省略しても、従業員の救済を妨げることはありません。何が起こるかというと、従業員レコードは保存されますが、個人レコードは保存されません(検証のため)。

検証エラーをフラッシュオブジェクトに表示するにはどうすればよいですか?

4

1 に答える 1

0

私が必要としていたのはvalidates_associated、次のようなものでした。

employee.rb

validates_associated :user

user.rb

validates_associated :person
于 2010-03-26T10:51:54.470 に答える