如何在View内部显示私人文件夹中的图像? [英] How to display image from private folder inside View?

查看:70
本文介绍了如何在View内部显示私人文件夹中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对似乎很简单的内容进行快速提示.我在私人文件夹中有一些图片,想在我的视图"中显示它们.

I need a quick tip on something which seems really simple. I have some pictures inside private folder and would like to display them inside my View.

我发现的唯一解决方案是:

The only solution I found was this:

def show
    send_file 'some/image/url', :disposition => 'inline', :type => 'image/jpg', :x_sendfile => true
end

我已阅读到:disposition => 'inline'不应触发图像下载,并允许我在View中显示它.问题在于,每次触发show操作时,图像下载都会自动激活并自动下载. show操作的视图未显示.

I've read that :disposition => 'inline' should not trigger image download and allow me to display it inside my View. The problem is that each time I trigger show action, image download is automatically activated and it is automatically downloaded. View for show action is not displayed.

如何在视图"中显示该图像?谢谢.

How can I display that image inside my View? Thank you.

推荐答案

我这样做的方式(并不是说这本书很完美)是我为图像创建了根,并在控制器中进行了操作以进行渲染它.

The way I do it, and I'm not saying it's perfectly by the book, is I make a root for images and an action in the controller to render it.

例如,在routes.rb中

So, for instance, in routes.rb

match '/images/:image', to: "your_controller#showpic", via: "get", as: :renderpic

在您的控制器中:

def showpic
    send_file "some/path/#{params[:image]}.jpg", :disposition => 'inline', 
              :type => 'image/jpg', :x_sendfile => true # .jpg will pass as format
end

def show
end

在您看来

<img src="<%= renderpic_path(your image) %>">

这是一个可行的示例,"send_file"上的参数较少

Here is a working example, with fewer parameters on "send_file"

def showpic
    photopath = "images/users/#{params[:image]}.jpg"
    send_file "#{photopath}", :disposition => 'inline'
end

这篇关于如何在View内部显示私人文件夹中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆