Forum: Ruby on Rails attachment_fu, polymorphic model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
71bc5fc6f37ff015781a4a8b4cc20106?d=identicon&s=25 Walksalong (Guest)
on 2007-05-10 21:43
(Received via mailing list)
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

Thanks,
Walksalong
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-05-10 22:14
(Received via mailing list)
>    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 Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
71bc5fc6f37ff015781a4a8b4cc20106?d=identicon&s=25 Walksalong (Guest)
on 2007-05-14 16:21
(Received via mailing list)
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.
This topic is locked and can not be replied to.