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/.
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.
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
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 :)
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.