Reputation: 265
So I just added paperclip and migrated it to a table called users_pictures. My Model looks like this:
class UsersPicture < ActiveRecord::Base
attr_accessible :user_id, :users_picture_file, :users_picture_id,
:photo_file_name, :photo_content_type, :photo_file_size
belongs_to :users
has_attached_file :photo,
:url => "/images/:attachment/:basename.:extension",
:path => ":rails_root/public/images/:attachment/:basename.:extension"
end
So when I am accessing the "new.html.erb" page, I can add a picture to the form ... give it the user_id and submit. Rails does not show me any errors and the log even says:
Processing by UsersPicturesController#create as HTML
Parameters: {"commit"=>"Create Users picture",`
"authenticity_token"=>"0tbAZj5MKyN/uaj0ybaNsa0dZrxeS05OJNNA3ZNO8Uc=",
"users_picture"=>{"user_id"=>"12",
"photo"=>#<ActionDispatch::Http::UploadedFile:0xe028dbd4
@content_type="image/jpeg", @tempfile=#<File:/tmp/RackMultipart20121019-14239-1edlwpp-0>,
@headers="Content-Disposition: form-data; name=\"users_picture[photo]\"; filename=\"rh.jpeg
\"\r\nContent-Type: image/jpeg\r\n", @original_filename="rh.jpeg">}, "utf8"=>"✓"}
[paperclip] Saving attachments.
Redirected to xxxx/users_pictures/8
Completed 302 Found in 18ms (ActiveRecord: 1.2ms)
But the fileinfo is not being saved into database ... so I cannot display it. Firebug tells me something about the parameters for the POST method:
authenticity_token 0tbAZj5MKyN/uaj0ybaNsa0dZrxeS05OJNNA3ZNO8Uc=
users_picture[user_id] 12
users_picture[photo] ÿØÿà�JFIF������ÿÛ�� !"&# /!$'),,-180*5*+,) 0%"5)-,*4,,,*,),),,,,,,,,,,,,,))),,,*,),,,,),),,,)).,ÿÀ��Ì�Ì"�ÿÄ��������������ÿÄ�E� ����!1AQ"aq2#BRbr¡3²¢±ÁÑ4SÓ%Cs£ÒáÿÄ������������ÿÄ�%��������!1"A2BQq¡ÿÚ���?�Þ4¥()JJRR ¥pLªXl °ôR|¨u]Þýú
And so on. So it is not saving the path into the database but the sourcecode of the picture? As you can see, I already set :url and :path (even without the :attachment because I don't know what it is)
In general, I just wanted to upload pictures to a certain path; and store the file information like name and size in my database.
My form from the view-folder looks like this:
<%= form_for(@users_picture, :html => { :multipart => true }) do |f| %>
...
<%= f.file_field :photo %>
I will not post the show and index.html.erb because displaying is another thing - I first have to achieve to save all the data in the database + upload the pictures to certain folders. Any advice on how to do it correctly ?
Upvotes: 2
Views: 2132
Reputation: 1221
You don't need to set attr_accessible
on those subfields. The only thing you need to mark as attr_accessible
is the :photo
attribute:
class UsersPicture < ActiveRecord::Base
attr_accessible :user_id, :users_picture_file, :users_picture_id, :photo
Upvotes: 1