Hi,
vielleicht kann mir jemand weiterhelfen. Habe das Problem, dass ich eine
polymorphische Beziehung zwischen zwei Modellen angelegt habe und diese
in einem Formular bearbeitet werden:
Model Account (mit Account-Typ, in diesem Fall z.B. Employee)
Model Employee (besitzt 1 Account)
Das Problem: Ich finde keine sinnvolle Möglichkeit, dass die Fehler
durch Validierung beider Modelle bei “Neu Anlegen” und/oder “Updaten”
gleichzeitig angezeigt werden. Es ist so, dass z.B. zuerst Employee
vollständig validiert wird und erst im Anschluss der zugehörige Account
(dessen Fehler sind erst ersichtlich, wenn Employee korrekt ist).
Ein “validates_associated :account” in Employee funktioniert bei mir nur
für “create” ordnungsgemäß - leider erhalte ich dann aber zusätzlich
eine Fehlermeldung, die keine Relevanz für den Nutzer hat (“is
invalid”).
Hoffe ich hab es halbwegs verständlich erklärt ;). Danke bereits für
eventuelle Hilfen!
Code-Beispiel aus employees_controller.rb
def new
@employee = Employee.new
@account = Account.new
respond_to do |format|
format.html
format.xml { render :xml => @employee }
end
end
def edit
access_to_profile
@employee = Employee.find(params[:id])
end
def create
@employee = Employee.new(params[:employee])
@account = @employee.build_account(params[:account])
respond_to do |format|
if (@employee.save)
# funktioniert nur durch "validates_associated" so, dass
@account ebenfalls gespeichert wird
flash[:notice] = I18n.t ‘notice_employee_new’
format.html { redirect_to(admin_employees_url) }
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
def update
access_to_profile
@employee = Employee.find(params[:id])
@account = Account.find(@employee.account)
respond_to do |format|
if (@employee.update_attributes(params[:employee]) &&
@account.update_attributes(params[:account]))
flash[:notice] = I18n.t ‘notice_employee_edit’
format.html { redirect_to(admin_employees_url) }
format.xml { head :ok }
else
format.html { render :action => “edit” }
format.xml { render :xml => @employee.errors, :status =>
:unprocessable_entity }
end
end
end