Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 17th, 2012  |  syntax: None  |  size: 1.19 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Polymorphic Nested Form with Paperclip in Rails 3.1.3
  2. class NewsItem < ActiveRecord::Base
  3.   has_many :file_uploads, :as => :uploadable
  4.   accepts_nested_attributes_for :file_uploads, :allow_destroy => true
  5. end
  6.  
  7.  
  8. class FileUpload < ActiveRecord::Base
  9.   belongs_to :uploadable, :polymorphic => true
  10.   has_attached_file :upload, :styles => {:thumb => '100x100>'}
  11. end
  12.        
  13. ...
  14.  
  15. <%= f.fields_for :file_uploads do |upload| %>
  16.   <div class="file_upload">
  17.     <%= upload.file_field :upload %>
  18.   </div>
  19.  
  20.   ...
  21.  
  22. <% end %>
  23.  
  24. ...
  25.        
  26. {"utf8"=>"✓",
  27.  "authenticity_token"=>"MBfxJ4XTizCXv3Mpu971VHCm60bS3Y84Kdxfe+VJD2w=",
  28.  "news_item"=>{"title"=>"",
  29.  "body"=>"",
  30.  "published_date"=>"",
  31.  "file_uploads_attributes"=>{"0"=>{"upload"=>#<ActionDispatch::Http::UploadedFile:0x000001070112a8 @original_filename="rails-dd352fc2630e5f9aa5685ef1d7fe5997.png",
  32.  @content_type="image/png",
  33.  @headers="Content-Disposition: form-data; name="news_item[file_uploads_attributes][0][upload]"; filename="rails-dd352fc2630e5f9aa5685ef1d7fe5997.png"rnContent-Type: image/pngrn",
  34.  @tempfile=#<File:/var/folders/hb/2bkct63171lck8d3sg0xfq0c0000gn/T/RackMultipart20111204-3216-71in7a>>,
  35.  "name"=>"",
  36.  "caption"=>""}}},
  37.  "commit"=>"Create News item"}