2011-01-07 47 views
0

我想用Heroku使用Paperclip。是的,根据Heroku's Application Constraints。我将能够将上传的文件存储到/ tmp或/.log文件夹中,该文件夹完全位于/ public文件夹之外。让Rails'image_tag'浏览/ public文件夹外的图片?

如果我们不打算谈论亚马逊S3,我怎么能访问图像中的/ tmp目录与IMAGE_TAG标签。

这是我的照片模式,即使用回形针

class ObbProductphoto < ActiveRecord::Base 
belongs_to :product 

has_attached_file :photo, :styles => {:high => '1024x768', :medium => '640x480', :thumb => '100x100'}, 
:path => "tmp/:id.:extension", 
:url => "tmp/:id.:extension" 
end 

这是我在浏览器中得到:

<img src="/images/tmp/24.JPG?1294433924" alt="24" /> 

它仍然使用/ images文件夹,

我试图破解任何/..../,无法获得解决方案。

谢谢你们, Suebphatt

回答

2

假设你可以把文件放在比/ tmp中更持久,存储和访问public目录之外上传的文件是可以使用:url和:路径参数,有位配置工作一起。

从代码中提取一个例子,我最近写的(它的修改,以至于如果你逐字复制可能无法正常工作):

app/models/picture.rb 
    # Define the attachment 
    has_attached_file :image, 
        :styles => {:large => ["700",  :jpg], 
           :thumb => ["100x100>", :gif]}, 
        :url => "/asset/picture/:id/:style/:basename.:extension", 
        :path => ":base/picture/:id/:style/:basename.:extension" 

config/initializers/storage.rb 
    # Configure common attachment storage 
    # This approach allows more flexibility in defining, and potentially moving, 
    # a common storage root for multiple models. If unneeded, just replace 
    # :base in the :path parameter with the actual path 
    Paperclip.interpolates :base do |attachment, style| 
    /path/to/persistent/storage 
    # A relative path from the Rails.root directory should work as well 
    end 

app/controllers/pictures_controller.rb 
    # Make the attachment accessible 
    def asset 
    instance = Picture.find(params[:id]) 
    params[:style].gsub!(/\.\./, '') 
    #check permissions before delivering asset? 
    send_file instance.image.path(params[:style].intern), 
       :type => instance.image_content_type, 
       :disposition => 'inline' 
    end 

config/routes.rb 
    # Add the necessary route 
    match '/asset/picture/:id/:style/:basename.:extension', :to => 'pictures#asset' 

app/views/pictures/show.html.erb 
    <% # Display the picture %> 
    <%= image_tag picture.image.url(:large) %> 

注意,这是所有的Rails 3的语法。在Rails 2.x中使用它需要进行一些更改,但希望能得到这个图片。

0

据我所知,你不能 - /tmp未配置为的东西,你可以成为项目出来(它是用于临时存储) 。

你可以在浏览器的/tmp网址上访问图片吗?做他们的工作?

+0

您可以在浏览器的/ tmp网址上访问图片吗?做他们的工作? ------我不能。无论如何,感谢您的建议:) – howdy 2011-01-08 06:53:51