在专用存储文件夹的rails 3.1中显示带有载波的图像 [英] Showing images with carrierwave in rails 3.1 in a private store folder
问题描述
我有一个Rails 3.1应用程序,正在添加载波来存储图像。但我想将这些图像存储在公共文件夹之外,因为只有在用户加载到应用程序中时,才可以访问它们。因此,我使用初始化文件更改了carrerwave中的store_dir:
I have a rails 3.1 app and I am adding carrierwave to store images. But I want to store those images outside the public folde,r because they should only be accessible when users are loaded in the app. So I changed the store_dir in carrerwave with the initializer file:
CarrierWave.configure do |config|
config.root = Rails.root
end
然后我的载波上传器转到像这样:
And my carrierwave uploader goes like this:
class ImageUploader < CarrierWave::Uploader::Base
...
def store_dir
"imagenes_expedientes/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
图像存储正确,如果使用公用文件夹,一切正常。但是,当尝试将内容移动到专用文件夹时,不显示图像,并且当我尝试在新窗口中打开它们时,出现以下错误:
Images are stored correctly and if I use the public folder everything works fine. However when trying to move things to the private folder, images are not displayed and when I try to open them in a new window I get the following error:
Routing Error
No route matches [GET] "/imagenes_expedientes/volunteer/avatar/15/avatar.jpg"
我试图通过控制器使用send_file传递文件,但我没有加载页面,而只是下载了图像。
I was trying to deliver the files using send_file through the controller, but instead of loading the page I only get the image downloaded.
def show
send_file "#{Rails.root}/imagenes_expedientes/avatar.jpg", :type=>"application/jpg", :x_sendfile=>true
end
最后,视图中将这样显示图像:
Finally Images are displayed like this in the views:
<%= image_tag(@volunteer.avatar_url, :alt => "Avatar", :class => "avatar round") if @volunteer.avatar? %>
这可能很容易解决,但是由于我是Rails的新手,所以我不知道该怎么做。我应该设定路线吗?还是可以使用send_file方法显示图像?
This may probably be solved rather easy, but since I am somehow new to Rails, I don´t know what to do it. Should I set a route? Or is there anyway to display the images using the send_file method?
谢谢!
我设法使用x-sendfile显示图像,并按照 clyfe 。我在控制器中做了一个新操作:
I managed to display images using x-sendfile and putting :disposition => 'inline' as suggested by clyfe. I made a new action in my controller:
def image
@volunteer = Volunteer.find(params[:id])
send_file "#{Rails.root}/imagenes_expedientes/#{@volunteer.avatar_url}",:disposition => 'inline', :type=>"application/jpg", :x_sendfile=>true
end
添加到路线中:
resources :volunteers do
member do
get 'image'
end
end
并显示在视图中:
<%= image_tag(image_volunteer_path(@volunteer), :alt => "Avatar", :class => "avatar round") if @volunteer.avatar? %>
希望对其他人有帮助!
推荐答案
:disposition => 'inline'
将使您的图像显示在浏览器中,而不是弹出下载对话框。
:disposition => 'inline'
will make your images display in the browser instead of popping up the download dialog.
def show
send_file "#{Rails.root}/imagenes_expedientes/avatar.jpg", :disposition => 'inline', :type=>"application/jpg", :x_sendfile=>true
end
取决于图像的大小和用户数量,您可能希望将此操作移至金属应用程序,或者将其移至另一个应用程序,以免阻塞服务器。
Depending on the images sizes and the number of users you might want to move this action to a metal app, or a separate so as to not block the server.
这篇关于在专用存储文件夹的rails 3.1中显示带有载波的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!