Forum: Ruby on Rails validate_presence_of not working for me

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
35594c037eba2fa48f7129d5fded828b?d=identicon&s=25 Peter Szinek (Guest)
on 2006-04-12 17:21
(Received via mailing list)
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
0a5d6fa912dda67f9b81017fbd9a81f2?d=identicon&s=25 Shane Sherman (Guest)
on 2006-04-12 18:01
(Received via mailing list)
Maybe validates_presence_of only checks if the value is not nil, and it
will
work fine if the value is a blank string.
35594c037eba2fa48f7129d5fded828b?d=identicon&s=25 Peter Szinek (Guest)
on 2006-04-12 18:16
(Received via mailing list)
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?
>> true

Please heeelp, i am really furious that i can not make this work.

Peter
49c38b11918618294f164df6cd9d84c1?d=identicon&s=25 Dan Perez (perezda)
on 2006-04-12 19:44
> 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?
35594c037eba2fa48f7129d5fded828b?d=identicon&s=25 Peter Szinek (Guest)
on 2006-04-12 20:38
(Received via mailing list)
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
5afd6080ad6ed43eb305e9e9a0309a74?d=identicon&s=25 David Willis (dawillis)
on 2006-04-12 20:44
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,
> Peter

Peter,

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.
35594c037eba2fa48f7129d5fded828b?d=identicon&s=25 Peter Szinek (Guest)
on 2006-04-12 21:00
(Received via mailing list)
> 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
49c38b11918618294f164df6cd9d84c1?d=identicon&s=25 Dan Perez (perezda)
on 2006-04-12 21:21
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...

Dan
35594c037eba2fa48f7129d5fded828b?d=identicon&s=25 Peter Szinek (Guest)
on 2006-04-12 21:27
(Received via mailing list)
Dan Perez wrote:
>
> 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
49c38b11918618294f164df6cd9d84c1?d=identicon&s=25 Dan Perez (perezda)
on 2006-04-12 21:44
> 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?

What does your view look like?

Dan
35594c037eba2fa48f7129d5fded828b?d=identicon&s=25 Peter Szinek (Guest)
on 2006-04-12 22:07
(Received via mailing list)
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
49c38b11918618294f164df6cd9d84c1?d=identicon&s=25 Dan Perez (perezda)
on 2006-04-12 22:13
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,
> Peter

No idea, still pretty new to this myself, but at least it works :-)
Dan
This topic is locked and can not be replied to.