Attachment_fu, polymorphic model


#1

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


#2
  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://weblog.techno-weenie.net
http://mephistoblog.com


#3

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.