Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

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

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 在私人商店文件夹中,在rails 3.1中显示载波的图像全部内容,希望文章能够帮你解决ruby-on-rails – 在私人商店文件夹中,在rails 3.1中显示载波的图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。