Forum: Ruby on Rails has_attachment and validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bolo (Guest)
on 2007-04-24 02:34
(Received via mailing list)
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
Antiarc (Guest)
on 2007-04-24 02:48
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! :))
Bolo (Guest)
on 2007-04-24 15:36
(Received via mailing list)
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 '}'
Rick O. (Guest)
on 2007-04-25 04:54
(Received via mailing list)
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/a...

--
Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
Bolo (Guest)
on 2007-04-25 10:47
(Received via mailing list)
I have to correct  the syntaxe. And now i have this error

 ArgumentError in EnseignantsController#index

wrong number of arguments (1 for 0)
Bolo (Guest)
on 2007-04-25 21:20
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>

2. 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 :(

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/a...
>
> --
> Rick O.
> http://lighthouseapp.com
> http://weblog.techno-weenie.net
> http://mephistoblog.com
Bill H. (Guest)
on 2008-02-18 07:24
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>
>
> 2. 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 :(
>
> 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/a...
>>
>> --
>> Rick O.
>> http://lighthouseapp.com
>> http://weblog.techno-weenie.net
>> http://mephistoblog.com
This topic is locked and can not be replied to.