# Attachment\_fu and polymorphism. Two models in one form

**URL:** <https://www.ruby-forum.com/t/attachment-fu-and-polymorphism-two-models-in-one-form/143327>\
**Category:** Rails\
**Created:** [July 17, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/attachment-fu-and-polymorphism-two-models-in-one-form/143327 "2008-07-17T12:36:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@chil](https://www.ruby-forum.com/u/chil)\
**Post date:** [July 17, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/attachment-fu-and-polymorphism-two-models-in-one-form/143327/1 "2008-07-17T12:36:44Z")

</div>

Hi!

I just discover attachment\_fu a few days ago.  
I made it work in a simple form.

Now I want to use a polymorphic relation.  
I want to attach a picture to a ‘association’ in the ‘association’ form.

## Models:

class Association \< ActiveRecord::Base  
has\_one :image, :as =\> :attachable, :dependent =\> :destroy  
end

class Image \< ActiveRecord::Base

belongs\_to :attachable, :polymorphic =\> true

has\_attachment :content\_type =\> :image,  
:storage =\> :file\_system,  
:max\_size =\> 500.kilobytes,  
:resize\_to =\> ‘320x200’,  
:thumbnails =\> { :thumb =\> [50,50] },  
:processor =\> :Rmagick

validates\_as\_attachment

end

## The controller:

[code=]def new  
@association = Association.new  
end

def create  
@association = Association.new(params[:association])  
image = @association.create\_image(params[:image])  
if @association.save and image.save  
flash[:notice] = ‘Association was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

## The view:

# CrÃ©ation d'une nouvelle association

\<% form\_tag :action =\> ‘create’ do %\>

```
    <%= error_messages_for :association %>
    <%= error_messages_for :image %>

```

Nom  
 \<%= text\_field 'association', 'nom' %\>

```
    <p><label for="association_description">Contenu de la

```

page:  
  
\<%= text\_area ‘association’, ‘description’ %\>

```
    <%= file_field 'image', :uploaded_data %>

```

\<%= submit\_tag “Create” %\>

\<% end %\>

\<%= link\_to ‘Back’, :action =\> ‘list’ %\>

But I get the error:  
undefined method `content\_type’ for “2083186.jpg”:String

I don’t understand how i can fix this.

Thanxs !

---

<div class="post-metadata">

**Author:** ![chil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@chil](https://www.ruby-forum.com/u/chil)\
**Post date:** [July 18, 2008, 9:12am UTC](https://www.ruby-forum.com/t/attachment-fu-and-polymorphism-two-models-in-one-form/143327/2 "2008-07-18T09:12:48Z")

</div>

On 17 Jul 2008, at 12:36, Achille P. wrote:

> ```
> <%= text_area 'association', 'description' %></p>
> 
> ```
> 
> undefined method `content\_type’ for “2083186.jpg”:String
> 
> I don’t understand how i can fix this.

File uploads need a multipart form:

\<% form\_tag :action =\> ‘create’, :html =\> {:multipart =\> true} do %\>

This is clearly stated on line 115 of the attachment\_fu README.

Best regards

Peter De Berdt
