Reputation: 2575
My models:
class NewsItem < ActiveRecord::Base
has_many :file_uploads, :as => :uploadable
accepts_nested_attributes_for :file_uploads, :allow_destroy => true
end
class FileUpload < ActiveRecord::Base
belongs_to :uploadable, :polymorphic => true
has_attached_file :upload, :styles => {:thumb => '100x100>'}
end
Form Code (nested within NewsItem)
...
<%= f.fields_for :file_uploads do |upload| %>
<div class="file_upload">
<%= upload.file_field :upload %>
</div>
...
<% end %>
...
On submit I get the following error: "unknown attribute: upload". Here are the params:
{"utf8"=>"✓",
"authenticity_token"=>"MBfxJ4XTizCXv3Mpu971VHCm60bS3Y84Kdxfe+VJD2w=",
"news_item"=>{"title"=>"",
"body"=>"",
"published_date"=>"",
"file_uploads_attributes"=>{"0"=>{"upload"=>#<ActionDispatch::Http::UploadedFile:0x000001070112a8 @original_filename="rails-dd352fc2630e5f9aa5685ef1d7fe5997.png",
@content_type="image/png",
@headers="Content-Disposition: form-data; name=\"news_item[file_uploads_attributes][0][upload]\"; filename=\"rails-dd352fc2630e5f9aa5685ef1d7fe5997.png\"\r\nContent-Type: image/png\r\n",
@tempfile=#<File:/var/folders/hb/2bkct63171lck8d3sg0xfq0c0000gn/T/RackMultipart20111204-3216-71in7a>>,
"name"=>"",
"caption"=>""}}},
"commit"=>"Create News item"}
I'm using Rails 3.1.3 and paperclip "~> 2.4".
Upvotes: 1
Views: 816
Reputation: 2575
Restarting the rails app fixed the problem. I'm guessing I installed the gem but did not restart, leading to the error above. Lesson learned: always restart after installing a gem.
Upvotes: 0
Reputation: 187
I'd avoid generic terms like "uploadable" because the resultant term "upload" has the potential for collision.
youavmatchulsky's suggestions are good too - if you have attr_accessible anywhere you'll need to make file_uploads_attributes accessible as well.
Also, the params don't look like the form is multipart, so I'd force it with :multipart => true in the call to form_for
EDIT: Even though it's supposed to happen automagically, you may have to explicitly accept_nested_attributes_for the join, and then on the join model accept_nested_attributes_for :uploadable -- I've found anaf to be pretty weird with things like polymorphic joins sometimes
Upvotes: 1