2013-03-04 97 views
2

我想通過paperclip & aws-sdk上傳文件s3。該文件正在成功保存到s3,但應用程序記錄始終包含零值。回形針不保存在應用程序分貝的s3文件路徑

附件型號:

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 

附件控制器:

class AttachmentsControllers < ApplicationController 

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

end 

日誌:

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") 

回答

0

這林e沒有意義:

Attachment.create(attachment: [:attachment]) 

你在做什麼傳遞給創建方法的散列。散列有一個密鑰attachment,它指向的值是一個只包含符號:attachment的數組。

您可能希望從發送到控制器操作的參數中獲取附件。事情是這樣的:

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

另外請注意,您應該遵循Rails的命名約定 - 爲模型附件應該被命名爲AttachmentsController

+0

道歉錯字錯誤控制器,更新現在 – loganathan 2013-03-04 21:23:39

+0

你能張貼'則params的內容'? – Jesper 2013-03-04 21:29:54