2011-12-11 92 views
5

我有一個路徑問題。在我的模型我有以下設置:如何將蝦PDF文件存儲到亞馬遜S3

class Pdffiles < ActiveRecord::Base 
    belongs_to :user 

    has_attached_file :invoice_file, 
        :path => ":rails_root/public/pdffiles/:user_id/:style/:basename.:extension", 
        :url => "/pdffiles/:user_id/:style/:basename.:extension", 

        :storage => :s3, 
         :bucket => '...', 
         :s3_credentials => { 
          :access_key_id => '...', 
          :secret_access_key => '...' 
         } 
end 

,並在控制器看起來我的行爲是:

pdf = Prawn::Document.new 
    pdf.move_down 70 

    pdf.text("Prawn Rocks") 
    pdf.render_file('prawn.pdf') 
    pdf_file = File.open('prawn.pdf') 

    pdff = Pdffile.new() 
    pdff.pdffile_file = pdf_file 
    pdff.user_id = todays_user.id 
    pdff.save 

而且我的問題是,這個PDF文件存儲在S3服務器,但在不好的地方。而目錄app/public/pdff/id_of_a_user/file_name_of_pdf_file是文件保存到

Users/my_name/my_ruby_root_directory/name_of_my_project/public/pdffiles/id_of_a_user/file_name_of_pdf_file

我不完全確定,如果我使用蝦來保存PDF文件的權利,但我認爲問題可能出現在控制器,我已經設置了位置,創建的文件必須保存在哪裏。

我想問你,爲了將PDF文件保存到S3的正確目錄中,我應該改變什麼......所有幫助將不勝感激!

曼尼的感謝,九月

回答

1

路徑的Users/my_name/my_ruby_root_directory/name_of_my_project/public部分來自的:rails_root/public部分非常容易您在回形針中配置的路徑。所以,如果你真的想S3的「目錄」是app/public/pdff/id_of_a_user/file_name_of_pdf_file你需要給回形針以下路徑:app/public/pdffiles/:user_id/:style/:basename.:extension

另外,根據你的模型,你應該使用pdff.invoice_file = pdf_file,而不是pdff.pdffile_file = pdf_file

我希望這有助於。