Hi all, I just begun with RoR and going through the Agile development w Rails depot example ATM. I did everything according to the book (i believe), and after 3 hours of googling, reading docs, making sure i did everything correctly i can not make the validate_presence_of() to work - at least not as it looks in the book. My Product model looks like: class Product < ActiveRecord::Base validates_presence_of :title end however, if i leave the title field empty, and push create, the newly created product is added to the list of products without any error message. What''s wrong? thanks, Peter
Maybe validates_presence_of only checks if the value is not nil, and it will work fine if the value is a blank string. On 4/12/06, Peter Szinek <peter@rt.sk> wrote:> > Hi all, > > I just begun with RoR and going through the Agile development w Rails > depot example ATM. I did everything according to the book (i believe), > and after 3 hours of googling, reading docs, making sure i did > everything correctly i can not make the validate_presence_of() to work - > at least not as it looks in the book. My Product model looks like: > > class Product < ActiveRecord::Base > validates_presence_of :title > end > > however, if i leave the title field empty, and push create, the newly > created product is added to the list of products without any error > message. What''s wrong? > > thanks, > Peter > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060412/b4d8e64f/attachment.html
Shane Sherman wrote:> Maybe validates_presence_of only checks if the value is not nil, and it > will work fine if the value is a blank string. >From the documentation:Validates that the specified attributes are not blank (as defined by Object#blank?). besides, the rails book suggests that it should work (i.e. if you don''t enter anything it will yell). btw, the rails book is ... i don''t know which rails version, but definitely not 1.1. Maybe this changed since 1.1? Object#blank? demo: peter@peter:~/development/workspaces/radrails/depot$ ruby script/console Loading development environment.>> "".blank? >> truePlease heeelp, i am really furious that i can not make this work. Peter
> Please heeelp, i am really furious that i can not make this work. > > PeterTry a different validation on a different attribute. or a different class. Does that work? -- Posted via http://www.ruby-forum.com/.
Dan Perez wrote:>> Please heeelp, i am really furious that i can not make this work. >> >> Peter > > Try a different validation on a different attribute. or a different > class. Does that work?Wow man, thanks for the tip. Hw did you know? Yes it works! Not that i now know what''s the problem with the original table ;-) any clues now? I did not do anything special, just created a simple model, and added this code: class Testtable < ActiveRecord::Base validates_presence_of :a, :b end An volia, it works perfectly! The validator is not even different, just the class and the attr. What should i do now? How is it possible that i did the same with the original stuff, (AFAIK) and ther it does not work? Please help, i would like to go on... thx, Peter
Peter Szinek wrote:> Hi all, > > I just begun with RoR and going through the Agile development w Rails > depot example ATM. I did everything according to the book (i believe), > and after 3 hours of googling, reading docs, making sure i did > everything correctly i can not make the validate_presence_of() to work - > at least not as it looks in the book. My Product model looks like: > > class Product < ActiveRecord::Base > validates_presence_of :title > end > > however, if i leave the title field empty, and push create, the newly > created product is added to the list of products without any error > message. What''s wrong? > > thanks, > PeterPeter, I tested validates_presense_of with this model: class Magazine < ActiveRecord::Base validates_presence_of :title end Here''s my console output:>> newmag = Magazine.create()=> #<Magazine:0xb7196904 @attributes={"title"=>nil}, @new_record=true, @errors=# <ActiveRecord::Errors:0xb717ba8c @base=#<Magazine:0xb7196904 ...>, @errors={"tit le"=>["can''t be blank"]}>>>> newmag = Magazine.create(:title => '''')=> #<Magazine:0xb7175470 @attributes={"title"=>""}, @new_record=true, @errors=#< ActiveRecord::Errors:0xb7174ae8 @base=#<Magazine:0xb7175470 ...>, @errors={"titl e"=>["can''t be blank"]}>>>> newmag = Magazine.create(:title => '' '')=> #<Magazine:0xb7171780 @attributes={"title"=>" "}, @new_record=true, @errors=# <ActiveRecord::Errors:0xb7170e0c @base=#<Magazine:0xb7171780 ...>, @errors={"tit le"=>["can''t be blank"]}>>>>I tried leaving :title as nil, as the empty string, as well as a string with a single space character. All three times the "can''t be blank" error is returned, and I confirmed that the magazines table does not get populated with a new record. I''m running Rails 1.1.2. The Agile Web Development with Rails Book was written to target V1.0, IIRC, but 1.0 wasn''t available when the book went to press, so the API descriptions target 1.0, and the code examples were tested against 0.13. I followed the depot example using 1.0 without issue. There must be something else wrong, but I don''t know what to suggest in trying to find it. Doesn''t the description field also have a validates_presence_of method? You could also try running the console and do: product = Product.new() product.image_url = ... product.price = ... product.description = ... product.save to see if the save fails that way. I hope you find the problem. -- Posted via http://www.ruby-forum.com/.
> I hope you find the problem.Howdy David, Thanks for the reply. However, as i wrote in my previous email, i created a new table and did the same thing as for product (only that the model is called differently and it has different attributes) and there it works... Any ideas how is this possible? Peter
Peter Szinek wrote:>> I hope you find the problem. > > Howdy David, > > Thanks for the reply. However, as i wrote in my previous email, i > created a new table and did the same thing as for product (only that the > model is called differently and it has different attributes) and there > it works... Any ideas how is this possible? > > PeterHey Peter, Well, since validations work, and you''re not getting any errors.... Are you 100% sure that the file you''re looking at is the one that is running? First time i worked on a test app i ran scaffold a bunch of times and I had a bunch of extra code lying around that I didn''t need... Dan -- Posted via http://www.ruby-forum.com/.
Peter Szinek
2006-Apr-12 19:26 UTC
[Rails] Re: Re: validate_presence_of not working for me
Dan Perez wrote:> Peter Szinek wrote: >>> I hope you find the problem. >> Howdy David, >> >> Thanks for the reply. However, as i wrote in my previous email, i >> created a new table and did the same thing as for product (only that the >> model is called differently and it has different attributes) and there >> it works... Any ideas how is this possible? >> >> Peter > > Hey Peter, > > Well, since validations work, and you''re not getting any errors.... > > Are you 100% sure that the file you''re looking at is the one that is > running? First time i worked on a test app i ran scaffold a bunch of > times and I had a bunch of extra code lying around that I didn''t need...Yes, because i have a [puts "i am here"] line in the product.rb, which i also changed to puts something different, and it worked immediately. It is possible it will be something really dummy, but not this... Peter
Dan Perez
2006-Apr-12 19:44 UTC
[Rails] Re: Re: Re: validate_presence_of not working for me
> Yes, because i have a [puts "i am here"] line in the product.rb, which i > also changed to puts something different, and it worked immediately. It > is possible it will be something really dummy, but not this... > > PeterWhat''s being stored in the db? null or a blank? What does your view look like? Dan -- Posted via http://www.ruby-forum.com/.
Peter Szinek
2006-Apr-12 20:05 UTC
[Rails] Re: Re: Re: validate_presence_of not working for me
Dan Perez wrote:>> Yes, because i have a [puts "i am here"] line in the product.rb, which i >> also changed to puts something different, and it worked immediately. It >> is possible it will be something really dummy, but not this... >> >> Peter > > What''s being stored in the db? null or a blank?Hey Dan, mysql> select * from products where title=''''; +----+-------+-------------+-----------+-------+---------------------+ | id | title | description | image_url | price | date_avalilable | +----+-------+-------------+-----------+-------+---------------------+ | 1 | | | | 0.00 | 2006-04-12 17:05:00 | | 2 | | | | 0.00 | 2006-04-12 17:19:00 | | 3 | | 56 | 95 | 0.00 | 2006-04-12 17:29:00 | | 4 | | | | 0.00 | 2006-04-12 17:30:00 | | 5 | | | | 0.00 | 2006-04-12 18:00:00 | +----+-------+-------------+-----------+-------+---------------------+ so ''''. Ugh. I just regenerated the scaffolding for product (because i overwrote it with the test) to show you my view and now it works. Unbelievable. How is this possible? What did the scaffold regeneration change to make it work now? Any idea? It _really_ did not work before i regenerated it. Anyway thx for your help, at least it works now (i would be happier though if i would know why) Thanks again, Peter
Dan Perez
2006-Apr-12 20:13 UTC
[Rails] Re: Re: Re: Re: validate_presence_of not working for me
Peter Szinek wrote:> How is this possible? What did the scaffold regeneration change to make > it work now? Any idea? It _really_ did not work before i regenerated it. > > Anyway thx for your help, at least it works now (i would be happier > though if i would know why) > > Thanks again, > PeterNo idea, still pretty new to this myself, but at least it works :-) Dan -- Posted via http://www.ruby-forum.com/.