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

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

  3. BearItAll

    BearItAll Guest

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

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 (here). After that, you can post your question and our members will help you out.