Rails validate in models

B

BearItAll

Hello.

I have been working through the Rails book. The given examples in the
tutorial works, but I paused at the validation section in the model to
make sure I understood what was happening.

I have looked at various examples to find out what I am doing wrong, but
as far as I can see I am treating data fields the way you all are.

The example from a product model goes like this,

class Product < ActiveRecord::Base

#various validation rules such as
validates_presence_of :title, :description

#Then in the 'def validate' I tried to add a few examples of my own,
def validate

@thisImage = pathtoimages + :image_url
errors.add:)image_url,"Image not found - #{:image_url}") unless
FileTest.exist?(@thisImage)

#where :image_url is just a file name.
end

end

I tried various ways to get the data of :image_url, but only end up with
the variable name image_url.

:image_url.to_s

@thisImage = "../../public/images/#{:image_url}"

But each one in the 'error.add' example above gives,

"Image url Image not found - ../../public/images/image_url"

Help me with this and I'll snog your face off, alternatively you can
select a free gift off the trolley.


i
 
K

Kevin Olbrich

Try this...

errors.add:)image_url,"Image not found - #{image_url}")=20

:symbols are not the same as variables.

Ummm..... I'll take the free gift.

_Kevin

-----Original Message-----
From: BearItAll [mailto:[email protected]]=20
Sent: Monday, September 05, 2005 06:06 AM
To: ruby-talk ML
Subject: Rails validate in models


Hello.

I have been working through the Rails book. The given examples in the
tutorial works, but I paused at the validation section in the model to =
make
sure I understood what was happening.

I have looked at various examples to find out what I am doing wrong, but =
as
far as I can see I am treating data fields the way you all are.

The example from a product model goes like this,

class Product < ActiveRecord::Base

#various validation rules such as
validates_presence_of :title, :description

#Then in the 'def validate' I tried to add a few examples of my own, =
def
validate
=09
@thisImage =3D pathtoimages + :image_url
errors.add:)image_url,"Image not found - #{:image_url}") unless
FileTest.exist?(@thisImage)

#where :image_url is just a file name.
end

end

I tried various ways to get the data of :image_url, but only end up with =
the
variable name image_url.

:image_url.to_s

@thisImage =3D "../../public/images/#{:image_url}"

But each one in the 'error.add' example above gives,

"Image url Image not found - ../../public/images/image_url"

Help me with this and I'll snog your face off, alternatively you can =
select
a free gift off the trolley.


i
 
B

BearItAll

Try this...

errors.add:)image_url,"Image not found - #{image_url}")

:symbols are not the same as variables.

Ummm..... I'll take the free gift.

_Kevin

Thank you.
Your solid plastic BIC biro is on its way via carrier sparrow.

(the snog may well of been a better option, I brushed my tooth last
week in preparation).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top