【问题标题】:Have Rails 'image_tag' to browse image outside the /public folder?有 Rails 'image_tag' 可以浏览 /public 文件夹之外的图像吗?
【发布时间】:2011-01-08 06:41:41
【问题描述】:

我正在尝试将 Paperclip 与 Heroku 一起使用。是的,根据Heroku's Application Constraints。我只能将上传的文件存储到 /tmp 或 /.log 文件夹中,它们完全位于 /public 文件夹之外。

如果我们不讨论 Amazon 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 文件夹,

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

谢谢你们, 苏伯法特

【问题讨论】:

    标签: ruby-on-rails-3 heroku paperclip


    【解决方案1】:

    假设您可以将文件放在比 /tmp 更永久的位置,则可以使用 :url 和 :path 参数以及一些配置工作。

    从我最近编写的代码中提取的一个示例(它已被修改得足够多,如果你逐字复制它可能无法正常工作):

    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 中使用它需要进行一些更改,但希望你能得到,嗯,图片。

    【讨论】:

      【解决方案2】:

      据我所知,您不能 - /tmp 没有配置为您可以提供物品的东西(它旨在用于临时存储)。

      您可以在浏览器中访问/tmp URL 上的图像吗?它们有效吗?

      【讨论】:

      • 您可以在浏览器的 /tmp URL 中访问图像吗?做他们的工作? ------ 我不能。无论如何,谢谢你的建议:)
      • 实际上,您可以从 tmp 文件夹中提供资产,尽管是暂时的。这是一个这样做的示例:devcenter.heroku.com/articles/using-compass 不幸的是,我无法找到专门针对图像执行此操作的示例,尽管我认为类似的方法应该可行。最好的用例是临时提供动态创建的图像。
      猜你喜欢
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多