Reputation: 7749
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.
However, now I'm trying to give my photos an attached file :image. I did everything Neath says in his tutorial, 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 =>
1 error prohibited this photo from being saved
There were problems with the following fields:
* Image file name must be set.
So what's going on here? Relevant code below:
class Photo < ActiveRecord::Base
attr_accessible :gallery_id, :name, :rating
belongs_to :gallery
validates_associated :gallery
has_attached_file :image
validates_attachment_presence :image
end
<% form_for [@gallery, @photo], :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<% if @photo.image? %>
<%= image_tag @photo.image.url %><br />
<%= link_to @photo.image.url, @photo.image.url %>
<% end %>
<%= f.label :image %><br />
<%= f.file_field :image %>
</p>
<p><%= f.submit %></p>
<% end %>
class Gallery < ActiveRecord::Base
attr_accessible :name, :user_id, :shoot_date
# destroy all photos when a gallery is destroyed
has_many :photos, :dependent => :destroy
end
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?
Update: here is the Mongrel output form the attempted upload transaction:
Processing PhotosController#update (for 127.0.0.1 at 2010-12-05 14:19:29) [PUT]
Parameters: {"photo"=>{"name"=>"blah", "image"=>#<File:/tmp/RackMultipart20101205-2909-wo2g7z-0>}, "commit"=>"Save changes", "id"=>"10", "gallery_id"=>"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]
Upvotes: 2
Views: 2470
Reputation: 7749
Figured it out. Threw the mongrel output into google and here we go:
http://railsforum.com/viewtopic.php?id=35544
Basically, they forget to tell you to add :image to the attr_accessible listing.
Photo model changed to
class Photo < ActiveRecord::Base
attr_accessible :gallery_id, :name, :rating, :image
belongs_to :gallery
validates_associated :gallery
has_attached_file :image
validates_attachment_presence :image
end
Upvotes: 3