微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 在私人商店文件夹中,在rails 3.1中显示载波的图像

我有一个rails 3.1应用程序,我正在添加载波来存储图像.但是,我想将这些图像存储在公共边框之外,因为只有当用户加载到应用程序中时才可以访问这些图像.所以我改变了store_dir在carrerwave与初始化程序文件
CarrierWave.configure do |config|
  config.root = Rails.root
end

我的载波上传器如下所示:

class ImageUploader < CarrierWave::Uploader::Base
...
def store_dir
  "imagenes_expedientes/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

图像存储正确,如果我使用公用文件夹,一切正常.但是,当尝试移动到私人文件夹,图像不显示,当我尝试在新窗口中打开它,我得到以下错误

Routing Error

No route matches [GET] "/imagenes_expedientes/volunteer/avatar/15/avatar.jpg"

我正在尝试通过控制器使用send_file传递文件,而不是加载页面,我只得到下载的图像.

def show
    send_file "#{Rails.root}/imagenes_expedientes/avatar.jpg",:type=>"application/jpg",:x_sendfile=>true
  end

最后图像在视图中显示如下:

<%= image_tag(@volunteer.avatar_url,:alt => "Avatar",:class => "avatar round") if @volunteer.avatar? %>

这可能会很容易解决,但由于我以某种方式新进Rails,我不知道该怎么做.我应该设置一条路线吗?还是有没有使用send_file方法显示图像?

谢谢!

回答

我设法使用x-sendfile显示图像,并将:disposition =>在clyfe建议的“内联”.我在我的控制器中做了一个新的动作:

def image
    @volunteer = Volunteer.find(params[:id])
    send_file "#{Rails.root}/imagenes_expedientes/#{@volunteer.avatar_url}",:disposition => 'inline',:x_sendfile=>true
  end

添加到路线:

resources :volunteers do
    member do
      get 'image'
    end 
  end

并在视图中显示

<%= image_tag(image_volunteer_path(@volunteer),:class => "avatar round") if @volunteer.avatar? %>

希望它帮助别人!

解决方法

disposition => “内联”将使您的图像在浏览器中显示,而不是弹出下载对话框.
def show
  send_file "#{Rails.root}/imagenes_expedientes/avatar.jpg",:x_sendfile=>true
end

根据图像大小和用户数量,您可能希望将此操作移动到金属应用程序,或单独的操作,以便不阻止服务器.

原文地址:https://www.jb51.cc/ruby/266907.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐