Prashant Tiwari
2006-Mar-17 06:34 UTC
[Rails] How to check for presence of particular value entered ?
Hi, I am entering value in textbox ("name") on form. Now name field in database is having ''unique'' constraint on it so that no 2 records with same name allowed in database. Now when I m trying to enter same name into database from my form I m getting following error. ===============================Mysql::Error: #23000Duplicate entry ''Prashant'' for key 2: INSERT INTO users (`name`, `password`) VALUES(''Prashant'', ''Twiari'') =============================== Instead of this error I want the Proper message should be displayed on same page ("Form page") with name textbox highlighted. How to do that? Also How to check presence of particular field value against the value I ve entered in form so that I can authenticate user for logging in? Please tell me . Thanx in advance. Prash -- Posted via http://www.ruby-forum.com/.
Craig White
2006-Mar-17 06:51 UTC
[Rails] How to check for presence of particular value entered ?
On Fri, 2006-03-17 at 07:34 +0100, Prashant Tiwari wrote:> Hi, > > I am entering value in textbox ("name") on form. Now name field in > database is having ''unique'' constraint on it so that no 2 records with > same name allowed in database. Now when I m trying to enter same name > into database from my form I m getting following error. > ===============================> Mysql::Error: #23000Duplicate entry ''Prashant'' for key 2: INSERT INTO > users (`name`, `password`) VALUES(''Prashant'', ''Twiari'') > ===============================> > Instead of this error I want the Proper message should be displayed on > same page ("Form page") with name textbox highlighted. How to do that? > Also How to check presence of particular field value against the value I > ve entered in form so that I can authenticate user for logging in? > > Please tell me .---- you need to do a little background work... start here... http://wiki.rubyonrails.org/rails/pages/UnderstandingValidation the Agile Web Development With Rails is an excellent source of all this information - you might want to buy the book in paper or PDF form from your bookseller or from here... http://www.pragmaticprogrammer.com/bookshelf/index.html Craig
John Tsombakos
2006-Mar-17 06:52 UTC
[Rails] How to check for presence of particular value entered ?
You want to use validates_uniqueness_of :name in your model definition. <http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000685> And it will be checked upon save of the record. jt On 3/17/06, Prashant Tiwari <tiwari_p_k@yahoo.com> wrote:> Hi, > > I am entering value in textbox ("name") on form. Now name field in > database is having ''unique'' constraint on it so that no 2 records with > same name allowed in database. Now when I m trying to enter same name > into database from my form I m getting following error. > ===============================> Mysql::Error: #23000Duplicate entry ''Prashant'' for key 2: INSERT INTO > users (`name`, `password`) VALUES(''Prashant'', ''Twiari'') > ===============================> > Instead of this error I want the Proper message should be displayed on > same page ("Form page") with name textbox highlighted. How to do that? > Also How to check presence of particular field value against the value I > ve entered in form so that I can authenticate user for logging in? > > Please tell me . > > Thanx in advance. > Prash > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
David Mitchell
2006-Mar-17 06:56 UTC
[Rails] How to check for presence of particular value entered ?
And as far as showing a nice error message goes, you might want to check out ''flash'' in the API... Regards Dave M. On 17/03/06, John Tsombakos <johnt519@gmail.com> wrote:> You want to use > validates_uniqueness_of :name > in your model definition. > > <http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000685> > > And it will be checked upon save of the record. > > jt > > On 3/17/06, Prashant Tiwari <tiwari_p_k@yahoo.com> wrote: > > Hi, > > > > I am entering value in textbox ("name") on form. Now name field in > > database is having ''unique'' constraint on it so that no 2 records with > > same name allowed in database. Now when I m trying to enter same name > > into database from my form I m getting following error. > > ===============================> > Mysql::Error: #23000Duplicate entry ''Prashant'' for key 2: INSERT INTO > > users (`name`, `password`) VALUES(''Prashant'', ''Twiari'') > > ===============================> > > > Instead of this error I want the Proper message should be displayed on > > same page ("Form page") with name textbox highlighted. How to do that? > > Also How to check presence of particular field value against the value I > > ve entered in form so that I can authenticate user for logging in? > > > > Please tell me . > > > > Thanx in advance. > > Prash > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >