# Attachment\_fu problem:

**URL:** <https://www.ruby-forum.com/t/attachment-fu-problem/172130>\
**Category:** Rails\
**Created:** [July 15, 2009, 10:08am UTC](https://www.ruby-forum.com/t/attachment-fu-problem/172130 "2009-07-15T10:08:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 15, 2009, 10:08am UTC](https://www.ruby-forum.com/t/attachment-fu-problem/172130/1 "2009-07-15T10:08:39Z")

</div>

Hi everyone,

I was trying to do image upload by using attachment\_fu but got no  
lucky. I have 2 models: album and cover. Each album is associated with  
only one cover. The database is like:

create\_table :covers do |t|  
t.integer :album\_id  
t.timestamps  
# the following fields are required by attachement\_fu  
t.integer :parent\_id  
t.string :content\_type  
t.string :filename  
t.string :thumbnail  
t.integer :size  
t.integer :width  
t.integer :height  
end

create\_table :albums, :force =\> true do |t|  
t.integer :cover\_id  
t.string :title  
end

The form is like:  
\<% error\_messages\_for :album %\>  
\<% form\_for(@album, :html =\> { :multipart =\> true }) do |f| %\>

\<%= label :album, :title %\> \<%= f.text\_field :title %\>

\<%= label :album, :cover %\> \<%= f.file\_field :uploaded\_cover\_data %\> We accept JPEG, GIF, or PNG files up to 500 KB.

\<%= f.submit "Create" %\>

\<% end %\>

The controller is like:  
Class AlbumController \< ActiveController::Base  
…  
def create  
@album = Album.new(params[:album])

```
respond_to do |format|
  if @album.save_with_cover
    flash[:notice] = 'Album was successfully created.'
    format.html { redirect_to(@album) }
    format.xml { render :xml => @album, :status

```

=\> :created, :location =\> @album }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @album.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end  
end

The model is like:  
class Cover \< ActiveRecord::Base  
belongs\_to :album  
has\_attachment :content\_type =\> :image,  
:storage =\> :file\_system,  
:max\_size =\> 1.megabytes,  
:resize\_to =\> ‘384, 256\>’,  
:processor =\> “ImageScience”,  
:thumbnails =\> { :large =\> ‘96, 96\>’,  
:medium =\> ‘64, 64\>’,  
:small =\> ‘48, 48\>’}  
validates\_as\_attachment  
end

class Album \< ActiveRecord::Base  
has\_one :cover  
attr\_accessor :uploaded\_cover\_data  
def save\_with\_cover  
cover = Cover.new  
begin  
self.transaction do  
if uploaded\_cover\_data && uploaded\_cover\_data.size \> 0  
cover.uploaded\_data = uploaded\_cover\_data  
cover.thumbnails.clear  
self.cover = cover  
cover.save! # the problem occurred here  
end  
save!  
end  
rescue  
if cover.errors.on(:size)  
logger.info(“Uploaded image is too big (500-KB max).”)  
errors.add\_to\_base(“Uploaded image is too big (500-KB max).” )  
end  
if cover.errors.on(:content\_type)  
logger.info(“Uploaded image content-type is not valid.”)  
errors.add\_to\_base(“Uploaded image content-type is not  
valid.” )  
end  
false  
end  
end  
end

The problem lies in “cover.save!”. The failure causes the transaction  
to roll back, thus no cover is stored.  
The image I uploaded is 332\*433 with a size of 27KB. It’s in the range  
of size. Where can the problem be?  
I have spent several hours on it but no luck. Can anyone give some  
some clues on it?

Xiahong

---

<div class="post-metadata">

**Author:** ![Xiahong\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/3d9bf3/32.png) [@Xiahong\_G](https://www.ruby-forum.com/u/Xiahong_G)\
**Post date:** [July 15, 2009, 3:19pm UTC](https://www.ruby-forum.com/t/attachment-fu-problem/172130/2 "2009-07-15T15:19:16Z")

</div>

Just out of curiousity, what OS is hosting the Rails application?

And as a quick test, comment out the validates\_as\_attachment in your  
Cover class, and try it again… that should tell you if its the  
validation that is causing the save to fail.

If I recall, there are a number of tweaks out on the web to correct an  
issue with the validation… something akin to resetting the file/data  
pointer for reading the uploaded file, but this is all vague  
recollection from about a year ago for me.
