Has_attachment and validation


#1

Hello

I would like call the validation when the user submit a file only. i
tested this

validates_as_attachment unless self.uploaded_data.blank?

NoMethodError in EnseignantsController#create

undefined method `uploaded_data’ for Enseignant:Class

But no Working`

Can you help me thanks you


#2

Bolo wrote:

validates_as_attachment unless self.uploaded_data.blank?

NoMethodError in EnseignantsController#create

undefined method `uploaded_data’ for Enseignant:Class

You’re getting this because “self” is a Class reference, not an instance
reference.

Try this:

validates_as_attachment :if => Proc.new {|p| not p.uploaded_data.blank?
}

(I don’t know if it’ll work, but give it a shot! :))


#3

Hi

thanks you for quick answer

the error

SyntaxError in EnseignantsController#index

/Users/bmichelin/Documents/Perso/uag-histoire/site-web/app/models/
enseignant.rb:32: syntax error, unexpected kEND, expecting ‘}’


#4

I have to correct the syntaxe. And now i have this error

ArgumentError in EnseignantsController#index

wrong number of arguments (1 for 0)


#5

On 4/23/07, Antiarc removed_email_address@domain.invalid wrote:

Try this:

validates_as_attachment :if => Proc.new {|p| not p.uploaded_data.blank?
}

(I don’t know if it’ll work, but give it a shot! :))

Just peek at the source. It’s a simple method that calls 2
validations. Call them manually any custom way you want.

http://bs.techno-weenie.net/!source/2850/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb#105


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com


#6

Thanks for you answer. I tested to custom the validation

  1. with before_save
    def before_save
    unless self.filename.blank?
    validates_presence_of :size, :content_type
    validate :attachment_attributes_valid?
    end
    end

return me this error

NoMethodError in EnseignantsController#update

undefined method `validates_presence_of’ for #Enseignant:0x301e980

  1. overrides validates_as_attachment

    def validates_as_attachment
    if self.filename.blank?
    validates_presence_of :size, :content_type
    validate :attachment_attributes_valid?
    end
    end

return me

NoMethodError in EnseignantsController#update

undefined method `filename’ for Enseignant:Class.

I try to find the solution my self but is not easy :frowning:

Rick O. wrote:

On 4/23/07, Antiarc removed_email_address@domain.invalid wrote:

Try this:

validates_as_attachment :if => Proc.new {|p| not p.uploaded_data.blank?
}

(I don’t know if it’ll work, but give it a shot! :))

Just peek at the source. It’s a simple method that calls 2
validations. Call them manually any custom way you want.

http://bs.techno-weenie.net/!source/2850/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb#105


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com


#7

I got this to work with a combination of your methods. All of the below
code went straight into my model that used attachment_fu


validates_presence_of :size, :content_type, :if => Proc.new { |my_model|
my_model.should_i_validate? }

def before_save
if should_i_validate?
attachment_attributes_valid?
end
end


These two lines then take the place of validates_as_attachment, which
can be removed.

Not the prettiest, but then, I don’t think that there are very many
cases that justify this behavior. I figured it out so that I could have
one model that worked as an uploaded image or a templated image, but
ultimately decided to just remove this code and refactor the model so
that the attachment_fu stuff was separate from my templated image code.

Bill Harding
http://www.williambharding.com/blog

Bolo wrote:

Thanks for you answer. I tested to custom the validation

  1. with before_save
    def before_save
    unless self.filename.blank?
    validates_presence_of :size, :content_type
    validate :attachment_attributes_valid?
    end
    end

return me this error

NoMethodError in EnseignantsController#update

undefined method `validates_presence_of’ for #Enseignant:0x301e980

  1. overrides validates_as_attachment

    def validates_as_attachment
    if self.filename.blank?
    validates_presence_of :size, :content_type
    validate :attachment_attributes_valid?
    end
    end

return me

NoMethodError in EnseignantsController#update

undefined method `filename’ for Enseignant:Class.

I try to find the solution my self but is not easy :frowning:

Rick O. wrote:

On 4/23/07, Antiarc removed_email_address@domain.invalid wrote:

Try this:

validates_as_attachment :if => Proc.new {|p| not p.uploaded_data.blank?
}

(I don’t know if it’ll work, but give it a shot! :))

Just peek at the source. It’s a simple method that calls 2
validations. Call them manually any custom way you want.

http://bs.techno-weenie.net/!source/2850/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb#105


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com