# Object with many paperclip photos

**URL:** <https://www.ruby-forum.com/t/object-with-many-paperclip-photos/186037>\
**Category:** Rails\
**Created:** [March 30, 2010, 7:16pm UTC](https://www.ruby-forum.com/t/object-with-many-paperclip-photos/186037 "2010-03-30T19:16:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 30, 2010, 7:16pm UTC](https://www.ruby-forum.com/t/object-with-many-paperclip-photos/186037/1 "2010-03-30T19:16:49Z")

</div>

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|  
%\>

image description: \<%= photo\_form.text\_field :description %\>  
\<%= photo\_form.file\_field :image %\>

  
\<% end %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 31, 2010, 9:05am UTC](https://www.ruby-forum.com/t/object-with-many-paperclip-photos/186037/2 "2010-03-31T09:05:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 31, 2010, 9:32am UTC](https://www.ruby-forum.com/t/object-with-many-paperclip-photos/186037/3 "2010-03-31T09:32:17Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 31, 2010, 5:22pm UTC](https://www.ruby-forum.com/t/object-with-many-paperclip-photos/186037/4 "2010-03-31T17:22:28Z")

</div>

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|  
> %\>
> 
> image description: \<%= photo\_form.text\_field :description %\>  
> \<%= photo\_form.file\_field :image %\>
> 
>   
> \<% 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

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [March 31, 2010, 8:34pm UTC](https://www.ruby-forum.com/t/object-with-many-paperclip-photos/186037/5 "2010-03-31T20:34:28Z")

</div>

It should work but I would recommend using  
accepts\_nested\_attributes\_for :image.

Few screencasts:

> **[\#196 Nested Model Form Part 1 - RailsCasts](http://railscasts.com/episodes/196-nested-model-form-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

> **[\#197 Nested Model Form Part 2 - RailsCasts](http://railscasts.com/episodes/197-nested-model-form-part-2)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Best,  
Martin
