简体   繁体   English

Rails:回形针不保存在数据库中

[英]Rails: paperclip does not save in database

So I just added paperclip and migrated it to a table called users_pictures. 因此,我只添加了回形针并将其迁移到名为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. 因此,当我访问“ new.html.erb”页面时,可以在表单中添加图片...为其提供user_id并提交。 Rails does not show me any errors and the log even says: Rails不会显示任何错误,日志甚至显示:

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. 但是fileinfo没有保存到数据库中...因此我无法显示它。 Firebug tells me something about the parameters for the POST method: Firebug告诉我有关POST方法的参数的一些信息:

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) 如您所见,我已经设置了:url和:path(即使没有:attachment也是如此,因为我不知道它是什么)

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. 我不会发布show和index.html.erb,因为显示是另一回事-我首先必须实现将所有数据保存在数据库中并将图片上传到某些文件夹。 Any advice on how to do it correctly ? 关于如何正确执行任何建议?

You don't need to set attr_accessible on those subfields. 您无需在这些子字段上设置attr_accessible The only thing you need to mark as attr_accessible is the :photo attribute: 您需要标记为attr_accessible的唯一东西是:photo属性:

class UsersPicture < ActiveRecord::Base
  attr_accessible :user_id, :users_picture_file, :users_picture_id, :photo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM