简体   繁体   English

回形针未在应用程序数据库中保存s3文件路径

[英]Paperclip not saving the s3 file path in application db

I am trying to upload the file s3 by paperclip & aws-sdk. 我正在尝试通过回形针和aws-sdk上传文件s3。 The file is being saved to the s3 successfully but the application record is always contains nil value. 该文件已成功保存到s3,但应用程序记录始终包含nil值。

Attachment Model: 附件型号:

class Attachment < ActiveRecord::Base
 has_attached_file :attachment,
                    storage: :s3,
                    bucket: "test",
                    s3_credentials: "#{Rails.root}/config/s3.yml",
                    path: "test/:id/:style.:extension"
end

Attachments Controller: 附件控制器:

class AttachmentsControllers < ApplicationController

  def create
   Attachment.create(attachment: params[:attachment])
  end

end

Log: 日志:

SQL (1.0ms)  INSERT INTO "attachments" ("attacheable_id", "attacheable_type", "attachment", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["attacheable_id", nil], ["attacheable_type", nil], ["attachment", nil], ["created_at", Mon, 04 Mar 2013 20:17:47 UTC +00:00], ["updated_at", Mon, 04 Mar 2013 20:17:47 UTC +00:00]]

[paperclip] Saving attachments.
[paperclip] saving :test/11/original.jpg
[AWS S3 200 1.783004 0 retries] put_object(:acl=>:public_read,:bucket_name=>"test",:content_length=>21377,:content_type=>"image/jpeg",:data=>#<Paperclip::UploadedFileAdapter:0xb5b91f80 @target=#<ActionDispatch::Http::UploadedFile:0xb2812290 @original_filename="544494_328200280615189_1305908665_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"song[attachment]\"; filename=\"544494_328200280615189_1305908665_n.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20130305-9168-1brnfyd>>, @tempfile=#<File:/tmp/544494_328200280615189_1305908665_n20130305-9168-1r0pwtu.jpg>>,:key=>":test/11/original.jpg")  

This line doesn't make sense: 这行没有意义:

Attachment.create(attachment: [:attachment])

What your doing is passing in a hash to the create method. 您所做的是将哈希传递给create方法。 The hash has a key attachment which points at a value which is an array containing only the symbol :attachment . 哈希具有键attachment ,该attachment指向一个值,该值是仅包含符号:attachment的数组。

You probably want to get the attachment from the params sent in to the controller action. 您可能希望从发送到控制器动作的参数中获取附件。 Something like this: 像这样:

Attachment.create(attachment: params[:attachment])

Also note that you should follow Rails' naming convention - a controller for the model Attachment should be named AttachmentsController 还要注意,您应该遵循Rails的命名约定-模型Attachment的控制器应命名为AttachmentsController

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

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