【发布时间】:2011-06-07 21:54:25
【问题描述】:
我已经为 Rails 3 应用程序安装了回形针 gem。在开发模式下一切正常。但是,在生产模式下运行时,如果我上传一个文件然后尝试再次下载它,它会下载一个具有正确名称和扩展名的文件,但它是一个空文件。在服务器上查看时,文件确实被上传并且位于正确的目录中。 (我的应用程序根目录中有一个“上传”文件夹。)
有人遇到过这种情况吗?
我的模特:
# app/models/document.rb
class Document < ActiveRecord::Base
belongs_to :kase
has_attached_file :document, :path => (Rails.root + "uploads/:class/:kase_id/:id").to_s, :url => ":class/:id"
validates_attachment_presence :document
validates_attachment_content_type :document, :content_type => [
'application/pdf',
'image/png',
'image/jpeg',
'image/pjpeg',
'text/plain'
]
end
我的控制器:
# app/controllers/documents_controller.rb
class DocumentsController < ApplicationController
respond_to :html
before_filter :initialize_kase # Sets the @kase instance
def show
@document = @kase.documents.find(params[:id])
send_file @document.document.path, :filename => @document.document_file_name, :content_type => @document.document_content_type
end
end
还有我的初始化程序(设置上面 has_attached_file 中使用的 :kase_id 占位符:
# config/initializers/paperclip.rb
Paperclip.interpolates('kase_id') do |attachment, style|
"kases/#{attachment.instance.kase.id.to_s}"
end
我可能还应该提到,我作为嵌套控制器 (/kases/XX/documents/XX) 访问它。不知道有没有效果...
【问题讨论】:
-
你能发布你的上传器的配置并描述你正在使用的服务器吗?
-
具体来说,我们需要您的型号代码和下载文件的控制器代码。我以前遇到过这个问题并解决了。
-
很抱歉。我必须先运行才能添加代码。我更新了 OP。
标签: ruby-on-rails paperclip production-environment