# Attachment\_fu strange behaviour with file uploading

**URL:** <https://www.ruby-forum.com/t/attachment-fu-strange-behaviour-with-file-uploading/135148>\
**Category:** Rails\
**Created:** [April 18, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/attachment-fu-strange-behaviour-with-file-uploading/135148 "2008-04-18T13:29:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [April 18, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/attachment-fu-strange-behaviour-with-file-uploading/135148/1 "2008-04-18T13:29:42Z")

</div>

Hi,  
thr’s a strange behavior with attachment\_fu. when i upload a file,  
sometimes it gets uploaded without prb/error msg and sometimes the same  
file gives the error as size error msg. particularly, when i upload the  
next file immediately after another. can anybody help me in this? thanks  
in advance…

this is my mugshots model:-  
class Mugshot \< ActiveRecord::Base  
has\_attachment :content\_type =\> [‘application/vnd.ms-excel’],  
:storage =\> :file\_system,  
:max\_size =\> 5.megabytes,  
:path\_prefix=\>‘public/temp\_files’

# validates\_as\_attachment

def validate  
errors.add\_to\_base(“You must choose a file to upload”) unless  
self.filename  
unless self.filename == nil  
# Images should only be GIF, JPEG, or PNG  
[:content\_type].each do |attr\_name|  
enum = attachment\_options[attr\_name]  
unless enum.nil? || enum.include?(send(attr\_name))  
errors.add\_to\_base(“You can only upload .xls file”)  
end  
end  
# Images should be less than 5 MB  
[:size].each do |attr\_name|  
enum = attachment\_options[attr\_name]  
unless enum.nil? || enum.include?(send(attr\_name))  
errors.add\_to\_base(“file size should be smaller than 5 MB in  
size”)  
end  
end  
end  
end  
end

-Dhaval

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [April 18, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/attachment-fu-strange-behaviour-with-file-uploading/135148/2 "2008-04-18T13:48:05Z")

</div>

I prefer to hack attachment\_fu.rb in vendor/plugin/attachment\_fu/lib  
than make def validate in model. But here’s my correction for you :

class Mugshot \< ActiveRecord::Base  
has\_attachment :content\_type =\> [‘application/vnd.ms-excel’],  
:storage =\> :file\_system,  
:size =\> 0.megabyte…5.megabytes,  
:path\_prefix=\>‘public/temp\_files’  
validates\_as\_attachment

end

NOW, go to {RAILS\_ROOT}\vendor\plugins\attachment\_fu\attachment\_fu.rb

in line 405 :  
…

def attachment\_attributes\_valid?  
[:size, :content\_type].each do |attr\_name|  
enum = attachment\_options[attr\_name]  
errors.add attr\_name, ActiveRecord::Errors.add\_to\_base(“You can only  
upload .xls file”)unless enum.nil? || enum.include?(send(attr\_name))  
end  
end  
…

and refresh your server. What do you think?

Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [April 18, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/attachment-fu-strange-behaviour-with-file-uploading/135148/3 "2008-04-18T14:14:49Z")

</div>

hi Reinhart,  
hey, that worked!!! though “errors.add attr\_name,  
ActiveRecord::Errors.add\_to\_base(“You can only upload .xls file”)unless  
enum.nil? || enum.include?(send(attr\_name))” gave me NoMethod error, so  
i kept the default line back. that gave me default error msg for wrong  
upload. but that worked.  
and even if i put “:size =\> 0.megabyte…5.megabytes,” line in my  
code, that also worked fine 🙂  
now i could upload the files continuously… without error 😃

Thanks a ton Reinhart

-Dhaval
