Paperclip not saving images

Hi, rails newb here.

I'm trying to make a photo album app with rails. I'm using paperclip, because I need to upload multiple images at the same time. But when I upload a picture, paperclip isn't saving it. I'm not getting any errors, it just doesn't save it. I've gone through multiple tutorials trying to get one to work, but all of them end up with the same result. All the functionality that I want without the pictures being uploaded :frowning:

Album Model:

[code] class Album < ActiveRecord::Base   attr_accessible :name   validates_presence_of :name   has_many :photos, :dependent => :destroy

  accepts_nested_attributes_for :photos end [/code]

Photo Model:

[code] require 'paperclip'

class Photo < ActiveRecord::Base   belongs_to :album

  has_attached_file :data, :styles => { :medium => "300x300>", :thumb => "100x100>" },                     :url => "/images/photos/:id/:style_:basename.:extension",                     :path => ":rails_root/public/images/photos/:id/:style_:basename.:extension"   validates_attachment_content_type :data, :content_type => 'image/jpeg', :message => "has to be in jpeg format"

end [/code]

albums_helper

[code] <div class="photo">   <p>   <% form.fields_for :photos, photo, :child_index => (photo.new_record? ? "index_to_replace_with_js" : nil) do |photo_form| %>     <%= photo_form.file_field :data %>     <%= link_to_function "delete", "remove_field($(this), ('.photo'))" %><br/>   <% end %>   </p> </div> [/code]

_form.html.erb in views/ablums

[code] <% form_for @album, :html => { :multipart => true } do |f| %>   <%= f.error_messages %>   <p>     <%= f.label :name %><br />     <%= f.text_field :name %>   </p>   <div id="photos">      <% if @album.new_record? %>        <%= render :partial => 'photo', :locals => { :form => f, :photo => @album.photos.build } %>     <% end %>   </div>   <%= add_object_link("New Photo", f, @album.photos.build, "photo", "#photos") %>   <p><%= f.submit "Submit" %></p> <% end %> [/code]

_photo.html.erb in views/albums

[code] <div class="photo">   <p>   <% form.fields_for :photos, photo, :child_index => (photo.new_record? ? "index_to_replace_with_js" : nil) do |photo_form| %>     <%= photo_form.file_field :data %>     <%= link_to_function "delete", "remove_field($(this), ('.photo'))" %><br/>   <% end %>   </p> </div> [/code]

Try creating a photo manually, so in script/console type:

Photo.create(:data => File.new("/path/to/a/file"))

Also check the rails log file for any warning/error message, beware it can be hard to spot in all the lines that get printed.

Photo.create(:data => File.new("/home/kaspir/Pictures/backgrounds/bg.png")

didn't return anything.

this is my development log error, although I'm not exactly sure what it means.

Processing AlbumsController#create (for 127.0.0.1 at 2010-08-23 12:31:08) [POST]   Parameters: {"commit"=>"Submit", "authenticity_token"=>"wvWbubnwEq9X3JidZZscpDdjes9pAqBIPHyyI/Wj3ak=", "album"=>{"name"=>"test", "photos_attributes"=>{"index_to_replace_with_js"=>{"data"=>#<File:/tmp/RackMultipart20100823-6874-1nyzz0m-0>}}}} WARNING: Can't mass-assign these protected attributes: photos_attributes   e[4;35;1mAlbum Create (0.3ms)e[0m e[0mINSERT INTO "albums" ("name", "created_at", "updated_at") VALUES('test', '2010-08-23 16:31:08', '2010-08-23 16:31:08')e[0m Redirected to http://localhost:3000/albums/10 Completed in 18ms (DB: 0) | 302 Found [http://localhost/albums\]

Any advice given is EXTREMELY appreciated. Been working on this for 2 days, and it's driving me nuts

There is a WARNING message in the log. Can't see it? I told you it was hard to spot.

So to solve your problem or at least move a step forward, change Album so that it reads:

attr_accessible :name, :photos_attributes

Fernando Perez wrote:

There is a WARNING message in the log. Can't see it? I told you it was hard to spot.

So to solve your problem or at least move a step forward, change Album so that it reads:

attr_accessible :name, :photos_attributes

Awesome that solved it! Much appreciated :slight_smile:

Awesome that solved it! Much appreciated :slight_smile:

I fell into that trap a few times in the past, so now I can quickly notice it :wink:

Fernando Perez wrote:

I fell into that trap a few times in the past, so now I can quickly notice it :wink:

Well I will definitely have to remember this, because that was extremely frustrating haha.