Validate_presence_of not working for me

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.

Shane S. 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

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 :wink: 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 S. 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 D. 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.

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 S. 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

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?

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

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 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

Peter S. 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 :slight_smile:
Dan