# Attachment\_fu, polymorphic model

**URL:** <https://www.ruby-forum.com/t/attachment-fu-polymorphic-model/103843>\
**Category:** Rails\
**Created:** [May 10, 2007, 9:43pm UTC](https://www.ruby-forum.com/t/attachment-fu-polymorphic-model/103843 "2007-05-10T21:43:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![walksalong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4491bb/32.png) [@walksalong](https://www.ruby-forum.com/u/walksalong)\
**Post date:** [May 10, 2007, 9:43pm UTC](https://www.ruby-forum.com/t/attachment-fu-polymorphic-model/103843/1 "2007-05-10T21:43:24Z")

</div>

I’m borrowing this question from a user named Spencer on the Rails  
Forums. He clearly explains the issue we’re having:

"First of all, apologies, as I am probably missing something utterly  
obvious.

I am trying to set up a polymorphic assets model, with attachment\_fu.  
Essentially attachment\_fu is working as I can upload directly to the  
attachment model. However when I try and upload a file from another  
controller (linked polymorphically) I get errors about the  
content\_type etc. not being in the list.

I assume this means that the params array isn’t capturing the data?  
How can I correct this?  
in my asset model i have:

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

(and has\_attachment + validates\_as\_attachment)

in my product model I have:

1. has\_many :assets, :as =\> :attachable

in the create method in my products controller is am using:

1. @asset = @product.assets.build(params[:uploaded\_data])

In my new product form I have:

1. \<%= file\_field ‘asset’, ‘uploaded\_data’ %\>

A yaml dump of the form content

1. — !map:HashWithIndifferentAccess html: multiparttrue commit:  
Create product: !map:HashWithIndifferentAccess name: test body: test  
title: test green: test category\_ids: - “3” sidebar: “” description:  
test action: create controller: admin/products asset: !  
map:HashWithIndifferentAccess uploaded\_data: cm\_logo.jpg

The errors I get:  
Assets is invalid  
Content type is not included in the list  
Content type can’t be blank  
Size is not included in the list  
Size can’t be blank  
Filename can’t be blank

Any help greatly appreciated."  
[http://www.railsforum.com/viewtopic.php?pid=23135](http://www.railsforum.com/viewtopic.php?pid=23135)

Thanks,  
Walksalong

---

<div class="post-metadata">

**Author:** ![walksalong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4491bb/32.png) [@walksalong](https://www.ruby-forum.com/u/walksalong)\
**Post date:** [May 10, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/attachment-fu-polymorphic-model/103843/2 "2007-05-10T22:14:29Z")

</div>

> 1. @asset = @product.assets.build(params[:uploaded\_data])
> 
> In my new product form I have:
> 
> 1. \<%= file\_field ‘asset’, ‘uploaded\_data’ %\>

uploaded\_data is a single virtual attribute in attachment\_fu. Try:

…build(:uploaded\_data =\> params[:uploaded\_data])

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![walksalong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/4491bb/32.png) [@walksalong](https://www.ruby-forum.com/u/walksalong)\
**Post date:** [May 14, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/attachment-fu-polymorphic-model/103843/3 "2007-05-14T16:21:22Z")

</div>

Thanks for the answer Rick. I tried your solution, but  
validates\_as\_attachment keeps kicking it back saying the attachment is  
invalid. I’ve tried gifs and jpegs at small sizes. When I remove the  
validates\_as\_attachment everything is null.
