加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 21:44:52 所属栏目:百科 来源:网络整理
导读:我有一个rails 3.1应用程序,我正在添加载波来存储图像.但是,我想将这些图像存储在公共边框之外,因为只有当用户加载到应用程序中时才可以访问这些图像.所以我改变了store_dir在carrerwave与初始化程序文件: CarrierWave.configure do |config| config.root =
我有一个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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读