I''m validating a form that has two models: User and Person. On submit, the controller first checks the User information, then if user.save returns true it validates the Person information. However when validation fails for User, the information filled out in the Person form fields is erased! Any ideas on how to preserve the fields? ___________________ Ben Jackson Diretor de Desenvolvimento ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org http://www.incomumdesign.com
On Jun 8, 2005, at 6:15 PM, Ben Jackson wrote:> I''m validating a form that has two models: User and Person. On > submit, the controller first checks the User information, then if > user.save returns true it validates the Person information. However > when validation fails for User, the information filled out in the > Person form fields is erased! Any ideas on how to preserve the fields?Are you creating the person model with the attributes from the form? You should have something like this: @session[''person''] = Person.new(@params[''person'']) Of course, on a GET, you should create a blank instance of Person.> ___________________ > Ben Jackson > Diretor de Desenvolvimento > > ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org > http://www.incomumdesign.com > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Joseph Hosteny jhosteny-ee4meeAH724@public.gmane.org H: 412.362.8672 C: 412.418.6023
So writing to the session will automatically fill in those form fields with the attibutes? That seems like it would solve the problem. On Jun 8, 2005, at 7:24 PM, Joseph Hosteny wrote:> > On Jun 8, 2005, at 6:15 PM, Ben Jackson wrote: > >> I''m validating a form that has two models: User and Person. On >> submit, the controller first checks the User information, then if >> user.save returns true it validates the Person information. However >> when validation fails for User, the information filled out in the >> Person form fields is erased! Any ideas on how to preserve the >> fields? > > Are you creating the person model with the attributes from the form? > You should have something like this: > > @session[''person''] = Person.new(@params[''person'']) > > Of course, on a GET, you should create a blank instance of Person. > >> ___________________ >> Ben Jackson >> Diretor de Desenvolvimento >> >> ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org >> http://www.incomumdesign.com >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > Joseph Hosteny > jhosteny-ee4meeAH724@public.gmane.org > H: 412.362.8672 > C: 412.418.6023 > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Ben Jackson wrote:> I''m validating a form that has two models: User and Person. On submit, > the controller first checks the User information, then if user.save > returns true it validates the Person information. However when > validation fails for User, the information filled out in the Person form > fields is erased! Any ideas on how to preserve the fields? > ___________________ > Ben Jackson > Diretor de Desenvolvimento > > ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org > http://www.incomumdesign.comYou might try something like this in your action: @user.attributes = @params[:user] @person.attributes = @params[:person] if @request.post? invalid ||= !@user.valid? invalid ||= !@person.valid? unless invalid @user.save @person.save redirect_to :action => :some_action end end I''m assuming that you''re using a self-postback form/action and that by not doing a redirect you will show the same form with error messages and filled inputs from the @user and @person objects. I hope that helps. rgds, Dema -- http://dema.ruby.com.br - Rails development from a .NET perspective
On Jun 8, 2005, at 6:52 PM, Ben Jackson wrote:> So writing to the session will automatically fill in those form > fields with the attibutes? That seems like it would solve the problem. >Not quite :) Actually, I wrote this in haste as I was going out the door. Assuming that your forms use a variable called @person, then you can do: @person = @session[''person''] = new Person(@params[''person'']) The session really doesn''t have anything to do with it - it is the filling in of the @person object which gets picked up when the POST completes with a validation failure. The forms are filled out with the attributes in that object at the time. I just put the session in there since that is usually there as well. You can look at the salted login generator - I try to make sure that the forms always have as much data as possible when a failure occurs (but not too much - the password fields are blanked out by default). Joe> On Jun 8, 2005, at 7:24 PM, Joseph Hosteny wrote: > > >> >> On Jun 8, 2005, at 6:15 PM, Ben Jackson wrote: >> >> >>> I''m validating a form that has two models: User and Person. On >>> submit, the controller first checks the User information, then if >>> user.save returns true it validates the Person information. >>> However when validation fails for User, the information filled >>> out in the Person form fields is erased! Any ideas on how to >>> preserve the fields? >>> >> >> Are you creating the person model with the attributes from the >> form? You should have something like this: >> >> @session[''person''] = Person.new(@params[''person'']) >> >> Of course, on a GET, you should create a blank instance of Person. >> >> >>> ___________________ >>> Ben Jackson >>> Diretor de Desenvolvimento >>> >>> ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org >>> http://www.incomumdesign.com >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> Joseph Hosteny >> jhosteny-ee4meeAH724@public.gmane.org >> H: 412.362.8672 >> C: 412.418.6023 >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Joseph Hosteny jhosteny-ee4meeAH724@public.gmane.org H: 412.362.8672 C: 412.418.6023