# attachment\_fu, polymorphic model

**URL:** <https://discuss.rubyonrails.org/t/attachment-fu-polymorphic-model/12789>\
**Category:** rubyonrails-talk\
**Created:** [May 10, 2007, 7:42pm UTC](https://discuss.rubyonrails.org/t/attachment-fu-polymorphic-model/12789 "2007-05-10T19:42:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walksalong](https://avatars.discourse-cdn.com/v4/letter/w/4491bb/32.png) [@Walksalong](https://discuss.rubyonrails.org/u/Walksalong)\
**Post date:** [May 10, 2007, 7:42pm UTC](https://discuss.rubyonrails.org/t/attachment-fu-polymorphic-model/12789/1 "2007-05-10T19:42:08Z")

</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:

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

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

in my product model I have:

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

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

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

In my new product form I have:

&nbsp;&nbsp;&nbsp;1. \<%= file\_field 'asset', 'uploaded\_data' %\>

A yaml dump of the form content

&nbsp;&nbsp;&nbsp;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:** ![Rick\_Olson](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@Rick\_Olson](https://discuss.rubyonrails.org/u/Rick_Olson)\
**Post date:** [May 10, 2007, 8:13pm UTC](https://discuss.rubyonrails.org/t/attachment-fu-polymorphic-model/12789/2 "2007-05-10T20:13:47Z")

</div>

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

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

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

---

<div class="post-metadata">

**Author:** ![Walksalong](https://avatars.discourse-cdn.com/v4/letter/w/4491bb/32.png) [@Walksalong](https://discuss.rubyonrails.org/u/Walksalong)\
**Post date:** [May 14, 2007, 2:18pm UTC](https://discuss.rubyonrails.org/t/attachment-fu-polymorphic-model/12789/3 "2007-05-14T14:18:29Z")

</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.
