Hi I have two objects on a form that need validating. The user object is a child from the firm object and is normally automatically saved when the firm object is saved. Now if I submit an empty form I get a ''record invalid''. I guess due to submitting no data to the DB. If I enter ''firm'' data to bypass validation of the ''firm'' object, the user validation works ok and prevents an insert into the DB. How do I validate both child and parent before writing to the DB and having them marked as mandatory fields in the form? def signup @firm = Firm.new(@params[:firm]) @user = User.new(@params[:user]) @firm.users << @user @gmt_zones = GmtZone.find_all @countries = Country.find_all if @request.post? and @firm.save! @session[:user] = User.authenticate(@user.login, @params[:user][:password]) flash[''notice''] = "Signup successful" redirect_back_or_default :action => "welcome" else flash[''notice''] = "Signup successful" end end -- PC __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
i would highly recommend using the validates_ options in your models and letting your model do validation as it should be. as well you can use .valid? to ensure there valid before you try saving, though .save will automatically fill in errors to be used on your view. the 15 minute video on rubyonrails.com shows this off. id also highly recommend buying the agile book as it explains this nicely as well and more in depth. On 8/21/05, Philippe Creytens <pcreytens-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> Hi > I have two objects on a form that need validating. The > user object is a child from the firm object and is > normally automatically saved when the firm object is > saved. > > Now if I submit an empty form I get a ''record > invalid''. I guess due to submitting no data to the DB. > If I enter ''firm'' data to bypass validation of the > ''firm'' object, the user validation works ok and > prevents an insert into the DB. > > How do I validate both child and parent before writing > to the DB and having them marked as mandatory fields > in the form? > > def signup > @firm = Firm.new(@params[:firm]) > @user = User.new(@params[:user]) > @firm.users << @user > @gmt_zones = GmtZone.find_all > @countries = Country.find_all > > if @request.post? and @firm.save! > @session[:user] = User.authenticate(@user.login, > @params[:user][:password]) > flash[''notice''] = "Signup successful" > redirect_back_or_default :action => "welcome" > else > flash[''notice''] = "Signup successful" > end > end > > -- PC > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
I just bought the rails book, and Zachery speaks the truth. It cleared up 80% of my questions on how to use the framework -- esp. how to centralize authorization, how to properly use flash and know when to redirect vs render action, etc. The $35 I spent on the book has easily paid for itself in time. You''ll want to visit the website though for errata, as there are a few items that are somewhat confusing otherwise. http://books.pragprog.com/titles/rails/errata On 8/23/05, Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i would highly recommend using the validates_ options in your models > and letting your model do validation as it should be. as well you can > use .valid? to ensure there valid before you try saving, though .save > will automatically fill in errors to be used on your view. the 15 > minute video on rubyonrails.com shows this off. id also highly > recommend buying the agile book as it explains this nicely as well and > more in depth. > > > On 8/21/05, Philippe Creytens <pcreytens-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > Hi > > I have two objects on a form that need validating. The > > user object is a child from the firm object and is > > normally automatically saved when the firm object is > > saved. > > > > Now if I submit an empty form I get a ''record > > invalid''. I guess due to submitting no data to the DB. > > If I enter ''firm'' data to bypass validation of the > > ''firm'' object, the user validation works ok and > > prevents an insert into the DB. > > > > How do I validate both child and parent before writing > > to the DB and having them marked as mandatory fields > > in the form? > > > > def signup > > @firm = Firm.new(@params[:firm]) > > @user = User.new(@params[:user]) > > @firm.users << @user > > @gmt_zones = GmtZone.find_all > > @countries = Country.find_all > > > > if @request.post? and @firm.save! > > @session[:user] = User.authenticate(@user.login, > > @params[:user][:password]) > > flash[''notice''] = "Signup successful" > > redirect_back_or_default :action => "welcome" > > else > > flash[''notice''] = "Signup successful" > > end > > end > > > > -- PC > > > > __________________________________________________ > > Do You Yahoo!? > > Tired of spam? Yahoo! Mail has the best spam protection around > > http://mail.yahoo.com > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Brock Weaver [OBC]Technique
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I just wanted to pipe in here real quick, praising the pragmatists for their innovative DRM in the PDF version of the Agile book (and others they have). They don''t use conventional DRM but simply brand the book with your name all over it. This is far superior to Amazon''s method of Adobe DRM. I''m still trying to return "The Ruby Way" i downloaded from them that is so locked down that it feels like i paid $27 for the privilege of being treated like crap. I''ve already emailed them personally but I feel this deserves a more public applause. :) thank you for letting me own my PDF file. Regs, Derek - -- Derek Wyatt - C++ / Ruby / Unix Programmer http://derekwyatt.org -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (MingW32) iD8DBQFDCzyMwwHFeC88e2IRArzrAJ4+dw6B5JWZZgC+WbPYZcJqKJ1c4QCeJf63 X0KbsIt46ItvP5TOQ8dfJNM=s89Z -----END PGP SIGNATURE-----
The book "The Ruby Way" is available on http://safari.oreilly.com/. If you don''t know this is $15 per month for access to 10 books at a time (10 slots that must be filled for 30 days each), No DRM, they just don''t give you an easy way to view it offline. The selection of computer books is decent, I am more than satisfied for $15/month. But I agree the DRM on the Agile book is excellent. Affective against the lazy consumer and not a pain in the ass. On Tuesday 23 August 2005 09:11 am, Derek Wyatt wrote:> I just wanted to pipe in here real quick, praising the pragmatists > for their innovative DRM in the PDF version of the Agile book (and > others they have). They don''t use conventional DRM but simply brand > the book with your name all over it. This is far superior to > Amazon''s method of Adobe DRM. I''m still trying to return "The Ruby > Way" i downloaded from them that is so locked down that it feels > like i paid $27 for the privilege of being treated like crap. > > I''ve already emailed them personally but I feel this deserves a more > public applause. :) thank you for letting me own my PDF file. > > Regs, > Derek-- Quinn Harris (970) 759-5163 quinn-i1JSih8DuHelVyrhU4qvOw@public.gmane.org _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails