Rails validate in models

Discussion in 'Ruby' started by BearItAll, Sep 5, 2005.

  1. BearItAll

    BearItAll Guest

    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, Sep 5, 2005
    #1
    1. Advertising

  2. 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:]=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
    Kevin Olbrich, Sep 5, 2005
    #2
    1. Advertising

  3. BearItAll

    BearItAll Guest

    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


    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).
    BearItAll, Sep 5, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vinjvinj
    Replies:
    15
    Views:
    574
    Jeremy Sanders
    Nov 10, 2005
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    433
    Jean-Paul Calderone
    Nov 7, 2005
  3. David Heinemeier Hansson
    Replies:
    0
    Views:
    234
    David Heinemeier Hansson
    Dec 23, 2004
  4. Dr Nic
    Replies:
    3
    Views:
    127
    Rick DeNatale
    Sep 23, 2006
  5. malugu satyajit

    Modules in rails? ( controllers or models?)

    malugu satyajit, Dec 2, 2007, in forum: Ruby
    Replies:
    3
    Views:
    151
    Todd Benson
    Dec 3, 2007
Loading...

Share This Page