Paperclip not saving images

74 views
Skip to first unread message

Kaspir Ghost

unread,
Aug 23, 2010, 12:55:33 PM8/23/10
to rubyonra...@googlegroups.com
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 :(

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]
--
Posted via http://www.ruby-forum.com/.

Fernando Perez

unread,
Aug 23, 2010, 1:22:41 PM8/23/10
to rubyonra...@googlegroups.com
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.

Kaspir Ghost

unread,
Aug 23, 2010, 1:31:53 PM8/23/10
to rubyonra...@googlegroups.com
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
[4;35;1mAlbum Create (0.3ms) [0m [0mINSERT INTO "albums" ("name",
"created_at", "updated_at") VALUES('test', '2010-08-23 16:31:08',
'2010-08-23 16:31:08') [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

Fernando Perez

unread,
Aug 23, 2010, 2:16:30 PM8/23/10
to rubyonra...@googlegroups.com
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

Kaspir Ghost

unread,
Aug 23, 2010, 11:43:19 PM8/23/10
to rubyonra...@googlegroups.com
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 :)

Fernando Perez

unread,
Aug 24, 2010, 7:44:58 AM8/24/10
to rubyonra...@googlegroups.com
> Awesome that solved it! Much appreciated :)

I fell into that trap a few times in the past, so now I can quickly
notice it ;-)


--

http://digiprof.tv

Kaspir Ghost

unread,
Aug 25, 2010, 5:41:56 PM8/25/10
to rubyonra...@googlegroups.com
Fernando Perez wrote:
>
> I fell into that trap a few times in the past, so now I can quickly
> notice it ;-)
>

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

Reply all
Reply to author
Forward
0 new messages