object with many paperclip photos

I have an object that has multiple photos uploaded to it, using the paperclip plugin. I used a virtual attribute to have mutliple photos in the form but I can't figure out how to get the image to save. It works if I put <%= photo_form.file_field :image_file_name %> instead of just :image. If I use just the following, the image_file_name field is blank when it is saved.

  <% for photo in @analysis.photos %>     <% fields_for "analysis[photo_attributes]", photo do |photo_form| %>        <p>           image description: <%= photo_form.text_field :description %>                  <%= photo_form.file_field :image %>        </p>      <% end %>   <% end %>

Hello,

Could You post what is generated by the form?

Paperclip is enough smart with mass saving and when he has params[:model][:image] poiting to Your file through file_field :image it should just grab the file (from Rack TMP) and save it as a image.

Best, Martin

this is what is generated by the form. I separated the lines at 'Image 1' which is the value I entered in for the Image description but it shows NULL for the image attributes (image_file_name, etc)

Processing AnalysesController#create (for 127.0.0.1 at 2010-03-31 09:26:53) [POST]   Parameters: {"authenticity_token"=>"WuQMc +ntdX84NSUnXwRbjCXqdSQ0AMAK6VrsxUvv5kQ=", "analysis"=>{"author"=>"Author 1", "photo_attributes"=>[{"description"=>"Image 1", "image"=>"IMG_3132.JPG"}, {"description"=>"", "image"=>""}, {"description"=>"", "image"=>""}]}, "commit"=>"Create"}   Analysis Columns (2.3ms) SHOW FIELDS FROM `analyses`   Photo Columns (1.6ms) SHOW FIELDS FROM `photos`   SQL (0.2ms) BEGIN   Analysis Create (0.7ms) INSERT INTO `analyses` (`problematik`, `comment`, `technique`, `samplenbr`, `ref`, `worknbr`, `date`, `author`, `record`, `created_at`, `updated_at`, `images`) VALUES(NULL, NULL, NULL, '', '', '', NULL, 'Author 1', 0, '2010-03-31 07:26:53', '2010-03-31 07:26:53', NULL)   Photo Create (0.4ms) INSERT INTO `photos` (`description`, `author`, `created_at`, `updated_at`, `image_file_name`, `image_content_type`, `image_file_size`, `image_updated_at`, `analysis_id`) VALUES

('Image 1',

NULL, '2010-03-31 07:26:53', '2010-03-31 07:26:53', NULL, NULL, NULL, NULL, 14)

ES wrote:

I have an object that has multiple photos uploaded to it, using the paperclip plugin. I used a virtual attribute to have mutliple photos in the form but I can't figure out how to get the image to save. It works if I put <%= photo_form.file_field :image_file_name %> instead of just :image. If I use just the following, the image_file_name field is blank when it is saved.

  <% for photo in @analysis.photos %>     <% fields_for "analysis[photo_attributes]", photo do |photo_form| %>        <p>           image description: <%= photo_form.text_field :description %>                  <%= photo_form.file_field :image %>        </p>      <% end %>   <% end %>

I know in Rails 2.3.5 there's an easier way to do this, but I cheated a bit an used attribute_fu because of this cool helper method.

<% form_for :analysis, @analysis, :url => analysis_path, :html => { :multipart => true } do |analysis_form| %> <%= analysis_form.render_associated_form(@analysis.photos, :new => 4) %> <%= analysis_form.submit('Submit') %>

Then you just add a partial called _photo.html.erb inside your analysis view folder, and throw in

<%= analysis_form.label(:image) %> <%= analysis_form.file_field(:image) %>

In your model you would put

class Analysis < ActiveRecord::Base   has_many :photos, :attributes => true end

Hope that helps.

~Jeremy

It should work but I would recommend using accepts_nested_attributes_for :image.

Few screencasts:

Best, Martin