首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >让Rails 'image_tag‘浏览/public文件夹之外的图像?

让Rails 'image_tag‘浏览/public文件夹之外的图像?
EN

Stack Overflow用户
提问于 2011-01-07 21:24:09
回答 2查看 3.5K关注 0票数 0

我试着和Heroku一起用剪纸夹。是的,根据Heroku的应用约束的说法。我将只能将上传的文件存储到/tmp或/.log文件夹中,该文件夹完全位于/public文件夹之外。

如果我们不讨论亚马逊S3,我如何使用image_tag标签访问/tmp目录中的图像?

这是我的照片模型,用剪纸夹

代码语言:javascript
运行
复制
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

这就是我在浏览器里得到的:

代码语言:javascript
运行
复制
<img src="/images/tmp/24.JPG?1294433924" alt="24" />

它仍然使用/images文件夹,

我试图破解任何/.../,都找不到解决方案。

谢谢你们,苏培特

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-07 22:41:41

假设您可以将文件放置在比/tmp更持久的位置,则可以使用:url和:path参数以及一些配置工作来存储和访问public目录之外的上载文件。

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

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2011-01-07 21:47:55

据我所知,您不能- /tmp没有配置为您可以提供的项目之外(它是为临时存储)。

您能访问浏览器中的/tmp URL上的图像吗?它们有用吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4630437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档