2011-03-12 88 views
3

更新:我已經切換到CarrierWave(終於得到它的工作),所以雖然我仍然欣賞這個問題的答案,我將無法嘗試,如果他們真的工作,因爲我已經完全刪除DM-回形針從我的代碼。如何使用Sinatra,Datamapper,DM-Paperclip和S3?


那裏嗨,

我正在開發使用DataMapper的一個西納特拉,web應用,並正在尋求與S3作爲存儲添加一些上傳的功能。我試過CarrierWave,但我無法讓它工作,所以現在我正在嘗試dm-paperclip。這就是我現在所擁有的:

型號:

class Article 
    include DataMapper::Resource 
    include Paperclip::Resource 

    property :id,      Serial 
    property :created_at,    DateTime 
    property :updated_at,    DateTime 
    property :title,     String 
    property :body,      Text 

    has_attached_file :screenshot, 
        :storage   => :s3, 
        :s3_credentials => { 
         :access_key_id  => 'my-access-key-id', 
         :secret_access_key => 'my-secret_access-key', 
         :bucket    => 'my-bucket' 
        }, 
        :styles => { 
         :medium => "300x300>", 
         :thumb => "100x100>" 
        } 
end 

控制器:

post '/articles/create' do 
    @article = Article.new 
    @article.title  = params[:title] 
    @article.body   = params[:body] 
    @article.screenshot = params[:screenshot] 

    begin 
    @article.save 
    rescue DataMapper::SaveFailureError => e 
    puts "Error saving article: #{e.to_s} validation: #{@article.errors.values.join(', ')}" 
    rescue StandardError => e 
    puts "Got an error trying to save the article #{e.to_s}" 
    end 

    redirect '/articles' 
end 

然而,當我創建一個新的文章不保存任何東西到我的S3存儲和我不也不會有任何錯誤。

任何想法我做錯了什麼?

回答

2

嘿!請嘗試我的分支:https://github.com/solnic/dm-paperclip它包含許多已修復S3的問題。一兩個月內我會發布它。

+0

配置回形針拋出一個錯誤: 「未初始化的常量ActiveSupport :: Inflector」(我沒有使用ActiveSupport) – Marc 2011-03-13 12:45:47

+0

好的,我會更新它,所以它不需要ActiveSupport。 – solnic 2011-03-14 13:11:24

1

除了已經發布的解決方案之外,我想添加一個推薦。

根據我的經驗,使用DataMapper的raise_on_save_failure功能對調試選項沒有多大幫助。我建議你禁用該功能,並使用類似下面的代碼:

if model.save then 
    return model 
    else 
    error = String.new 
    model.errors.each do |e| 
     error << "#{e[0]}\n" 
    end 
    raise ArgumentError, error 
    end 

這樣的話,你會得到DM試圖堅持你的模型時遇到的每一個問題的完整說明。我發現它不僅適用於調試,還適用於嚮應用程序的消費者顯示這些消息。

0

前一段時間我特意爲S3做了叉子。我的分支與官方的AWS-SDK一起工作,而不是舊的aws-s3,它大多是過時的。

如果有人將搜索的回形針S3的解決方案,這是一個工作(今天)

https://github.com/krzak/dm-paperclip-s3

看看自述獲得如何爲S3

相關問題