Rails validate in models

(BearItAll) #1

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

(Kevin Olbrich) #2

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

···

-----Original Message-----
From: BearItAll [mailto:bearitall@rassler.co.uk]
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
  
  @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

(BearItAll) #3

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

···

On Mon, 05 Sep 2005 22:37:10 +0900, Kevin Olbrich wrote:

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