Jim Jeffers
2005-Aug-05 05:45 UTC
Newbie Question: Lets be straightforward with form building.
Ok guys, Rails is awesome. So awesome that while I''m in awe of how easy it is to do the PITA CRUD work I normally have to code that I don''t understand how to do the basics of working with forms. Here is my ordeal. I want to add a password confirmation field to my form. Then I want to validate that the confirmation password does in fact match my submitted password. Here is a snippet code in my add_user controller... <label>Username:</label> <%= text_field("user", "name") %> <label>Password:</label> <%= password_field("user", "password") %> <label>Confirm:</label> <%= password_field("user", "confirm_password") %> below is the error that I get thrown. I know I''m not creating the field the right way becuase I''m associating it with the User model. What should I do? This field is significant but it won''t need to be stored in the database. Also, how would I access this parameter from the controller or would I check it in the User model? NoMethodError in Login#add_user Showing /login/add_user.rhtml where line #18 raised: undefined method `confirm_password'' for #<User:0x24eda38> Extracted source (around line #18): 15: <%= password_field("user", "password") %> 16: 17: <label>Confirm:</label> 18: <%= password_field("user", "confirm_password") %> 19: 20: <div class="clear"></div> 21: </fieldset> _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ben
2005-Aug-05 05:58 UTC
Re: Newbie Question: Lets be straightforward with form building.
An easy way to work around this would be to make ''password'' and ''confirm_password'' non-db attributes of the user model, and have a ''pass'' field in the database for ActiveRecord to store. Then have the model validate and compare ''password'' and ''confirm_password'', store ''pass'' in the db, and wipe ''password'' and ''confirm_password'' (so they don''t get sent back to the client). That''s roughly how we do it on the brainguppy.com signup form. I hope this is fairly clear. Cheers, Ben Jim Jeffers wrote:> Ok guys, > > Rails is awesome. So awesome that while I''m in awe of how easy it is to > do the PITA CRUD work I normally have to code that I don''t understand > how to do the basics of working with forms. > > Here is my ordeal. I want to add a password confirmation field to my > form. Then I want to validate that the confirmation password does in > fact match my submitted password. > > > Here is a snippet code in my add_user controller... > > <label>Username:</label> > <%= text_field("user", "name") %> > > <label>Password:</label> > <%= password_field("user", "password") %> > > <label>Confirm:</label> > <%= password_field("user", "confirm_password") %> > > below is the error that I get thrown. I know I''m not creating the field > the right way becuase I''m associating it with the User model. What > should I do? This field is significant but it won''t need to be stored > in the database. > > Also, how would I access this parameter from the controller or would I > check it in the User model? > > *NoMethodError in Login#add_user* > > Showing //login/add_user.rhtml/ where line *#18* raised: > > undefined method `confirm_password'' for #<User:0x24eda38> > > > Extracted source (around line *#18*): > > 15: <%= password_field("user", "password") %> > 16: > 17: <label>Confirm:</label> > 18: <%= password_field("user", "confirm_password") %> > 19: > 20: <div class="clear"></div> > 21: </fieldset> > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Cynthia Kiser
2005-Aug-05 06:05 UTC
Re: Newbie Question: Lets be straightforward with form building.
Looks like you can rename the confirmation field password_confirm and then use the validates_confirmation_od method http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000639 Quoting Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org>:> Here is my ordeal. I want to add a password confirmation field to my > form. Then I want to validate that the confirmation password does in > fact match my submitted password. > > > Here is a snippet code in my add_user controller... > > <label>Username:</label> > <%= text_field("user", "name") %> > > <label>Password:</label> > <%= password_field("user", "password") %> > > <label>Confirm:</label> > <%= password_field("user", "confirm_password") %>-- Cynthia Kiser
Jim Jeffers
2005-Aug-05 07:34 UTC
Re: Newbie Question: Lets be straightforward with form building.
Wow that''s amazing!! I''m still having trouble though.. using this method brings up no errors... even if the passwords don''t match. Hmmm. On Aug 4, 2005, at 11:05 PM, Cynthia Kiser wrote:> Looks like you can rename the confirmation field password_confirm and > then use the validates_confirmation_od method > > http://api.rubyonrails.com/classes/ActiveRecord/Validations/ > ClassMethods.html#M000639 > > Quoting Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org>: > >> Here is my ordeal. I want to add a password confirmation field to my >> form. Then I want to validate that the confirmation password does in >> fact match my submitted password. >> >> >> Here is a snippet code in my add_user controller... >> >> <label>Username:</label> >> <%= text_field("user", "name") %> >> >> <label>Password:</label> >> <%= password_field("user", "password") %> >> >> <label>Confirm:</label> >> <%= password_field("user", "confirm_password") %> >> > > -- > Cynthia Kiser > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
San
2005-Aug-05 07:47 UTC
Re: Newbie Question: Lets be straightforward with form building.
Hi Jim, I''d suggest two changes: In your model add the following lines after the class declaration: class User < ActiveRecord::Base attr_accessor :password_confirm validates_confirmation_of :password [...more stuff here...] end and then in the form change ''confirm_password'' to ''password_confirm''. From the controller you can then create a new User object via: u = User.new(params[:user]) and then the validation will run automatically and highlight any form entry errors on the page if you do: if u.save #save succeeded redirect_to :action => ''login_successful'' #your action here return else #save failed, re-render the original form w/ error messages render :action => ''loginpage'' #your action here return end The ''validates_confirmation_of'' will automatically check to see if ''password'' and ''password_confirm'' are equal during User.save. The ''attr_accessor'' method will create a non DB backed attribute as part of the User class. So that''s the DRY way to do it - but that also means the form fields and the model have to line up with each other. If you want to go the manual route and fill in the user object by yourself, change the ''text_field'' and ''password_field'' helper calls to ''text_field_tag'' and ''password_field_tag'' calls. Then you can simply access the form field names via ''params[:fieldname]'' (e.g. u.password = params[:password]) -San Jim Jeffers wrote:> Ok guys, > > Rails is awesome. So awesome that while I''m in awe of how easy it is to > do the PITA CRUD work I normally have to code that I don''t understand > how to do the basics of working with forms. > > Here is my ordeal. I want to add a password confirmation field to my > form. Then I want to validate that the confirmation password does in > fact match my submitted password. > > > Here is a snippet code in my add_user controller... > > <label>Username:</label> > <%= text_field("user", "name") %> > > <label>Password:</label> > <%= password_field("user", "password") %> > > <label>Confirm:</label> > <%= password_field("user", "confirm_password") %> > > below is the error that I get thrown. I know I''m not creating the field > the right way becuase I''m associating it with the User model. What > should I do? This field is significant but it won''t need to be stored > in the database. > > Also, how would I access this parameter from the controller or would I > check it in the User model? > > *NoMethodError in Login#add_user* > > Showing //login/add_user.rhtml/ where line *#18* raised: > > undefined method `confirm_password'' for #<User:0x24eda38> > > > Extracted source (around line *#18*): > > 15: <%= password_field("user", "password") %> > 16: > 17: <label>Confirm:</label> > 18: <%= password_field("user", "confirm_password") %> > 19: > 20: <div class="clear"></div> > 21: </fieldset> > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jim Jeffers
2005-Aug-05 16:56 UTC
Re: Re: Newbie Question: Lets be straightforward with form building.
Thanks San! My inexperience really shows.. I''m just waiting for things to come together for me the more I work with this. I''m still not at the point where I can just think something and code it out like I do in PHP but I''m getting there. And the end result is a lot nicer in rails :D - Jim On Aug 5, 2005, at 12:47 AM, San wrote:> Hi Jim, > > I''d suggest two changes: > > In your model add the following lines after the class declaration: > > class User < ActiveRecord::Base > > attr_accessor :password_confirm > validates_confirmation_of :password > > [...more stuff here...] > end > > and then in the form change ''confirm_password'' to ''password_confirm''. > > > From the controller you can then create a new User object via: > > u = User.new(params[:user]) > > and then the validation will run automatically and highlight any > form entry errors on the page if you do: > > if u.save > #save succeeded > redirect_to :action => ''login_successful'' #your action here > return > else > #save failed, re-render the original form w/ error messages > render :action => ''loginpage'' #your action here > return > end > > The ''validates_confirmation_of'' will automatically check to see if > ''password'' and ''password_confirm'' are equal during User.save. > > The ''attr_accessor'' method will create a non DB backed attribute as > part of the User class. > > So that''s the DRY way to do it - but that also means the form > fields and the model have to line up with each other. > > If you want to go the manual route and fill in the user object by > yourself, change the ''text_field'' and ''password_field'' helper calls > to ''text_field_tag'' and ''password_field_tag'' calls. > > Then you can simply access the form field names via ''params > [:fieldname]'' > (e.g. u.password = params[:password]) > > -San > > > > Jim Jeffers wrote: > >> Ok guys, >> Rails is awesome. So awesome that while I''m in awe of how easy it >> is to do the PITA CRUD work I normally have to code that I don''t >> understand how to do the basics of working with forms. >> Here is my ordeal. I want to add a password confirmation field to >> my form. Then I want to validate that the confirmation password >> does in fact match my submitted password. >> Here is a snippet code in my add_user controller... >> <label>Username:</label> >> <%= text_field("user", "name") %> >> <label>Password:</label> >> <%= password_field("user", "password") %> >> <label>Confirm:</label> >> <%= password_field("user", "confirm_password") %> >> below is the error that I get thrown. I know I''m not creating the >> field the right way becuase I''m associating it with the User >> model. What should I do? This field is significant but it won''t >> need to be stored in the database. >> Also, how would I access this parameter from the controller or >> would I check it in the User model? >> *NoMethodError in Login#add_user* >> Showing //login/add_user.rhtml/ where line *#18* raised: >> undefined method `confirm_password'' for #<User:0x24eda38> >> Extracted source (around line *#18*): >> 15: <%= password_field("user", "password") %> >> 16: 17: <label>Confirm:</label> >> 18: <%= password_field("user", "confirm_password") %> >> 19: 20: <div class="clear"></div> >> 21: </fieldset> >> --------------------------------------------------------------------- >> --- >> _______________________________________________ >> 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 > > >