Note that there are some explanatory texts on larger screens.

plurals
  1. PORails and Paperclip with nested models: uploads from form not working
    primarykey
    data
    text
    <p>I'm currently trying to make a photo gallery application where photos are only accessible through a gallery interface. Gallery => has_many :photos, Photo => belongs_to :gallery. All of this is working fine.</p> <p>However, now I'm trying to give my photos an attached file :image. I did everything Neath says in <a href="http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails.html" rel="nofollow">his tutorial</a>, and I've just added validates_attachment_presence :image. Before the validation, the photo model was working fine except that after saving one with an image, the image never showed up. Now, with the validation, after selecting an image to upload, I'm getting a :flash => </p> <pre><code>1 error prohibited this photo from being saved There were problems with the following fields: * Image file name must be set. </code></pre> <p>So what's going on here? Relevant code below:</p> <h1>models/photo</h1> <pre><code> class Photo &lt; ActiveRecord::Base attr_accessible :gallery_id, :name, :rating belongs_to :gallery validates_associated :gallery has_attached_file :image validates_attachment_presence :image end </code></pre> <h1>views/photos/_form.html.erb</h1> <pre><code>&lt;% form_for [@gallery, @photo], :html =&gt; { :multipart =&gt; true } do |f| %&gt; &lt;%= f.error_messages %&gt; &lt;p&gt; &lt;%= f.label :name %&gt;&lt;br /&gt; &lt;%= f.text_field :name %&gt; &lt;/p&gt; &lt;p&gt; &lt;% if @photo.image? %&gt; &lt;%= image_tag @photo.image.url %&gt;&lt;br /&gt; &lt;%= link_to @photo.image.url, @photo.image.url %&gt; &lt;% end %&gt; &lt;%= f.label :image %&gt;&lt;br /&gt; &lt;%= f.file_field :image %&gt; &lt;/p&gt; &lt;p&gt;&lt;%= f.submit %&gt;&lt;/p&gt; &lt;% end %&gt; </code></pre> <h1>models/gallery.rb</h1> <pre><code>class Gallery &lt; ActiveRecord::Base attr_accessible :name, :user_id, :shoot_date # destroy all photos when a gallery is destroyed has_many :photos, :dependent =&gt; :destroy end </code></pre> <p>I think I've set up the multipart form correctly, and I think I've had this problem before when I tried a paperclip model without nesting models. Am I missing something?</p> <p>Update: here is the Mongrel output form the attempted upload transaction:</p> <pre><code>Processing PhotosController#update (for 127.0.0.1 at 2010-12-05 14:19:29) [PUT] Parameters: {"photo"=&gt;{"name"=&gt;"blah", "image"=&gt;#&lt;File:/tmp/RackMultipart20101205-2909-wo2g7z-0&gt;}, "commit"=&gt;"Save changes", "id"=&gt;"10", "gallery_id"=&gt;"3"} Gallery Columns (0.6ms) SHOW FIELDS FROM `galleries` Gallery Load (0.1ms) SELECT * FROM `galleries` WHERE (`galleries`.`id` = 3) Photo Columns (0.7ms) SHOW FIELDS FROM `photos` Photo Load (0.1ms) SELECT * FROM `photos` WHERE (`photos`.`id` = 10 AND (`photos`.gallery_id = 3)) WARNING: Can't mass-assign these protected attributes: image SQL (0.1ms) BEGIN CACHE (0.0ms) SELECT * FROM `galleries` WHERE (`galleries`.`id` = 3) SQL (0.1ms) ROLLBACK Rendering template within layouts/application Rendering photos/edit Rendered photos/_form (64.1ms) Completed in 83ms (View: 67, DB: 2) | 200 OK [http://localhost/galleries/3/photos/10] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload