我需要对似乎很简单的内容进行快速提示.我在私人文件夹中有一些图片,想在我的视图"中显示它们.
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内部显示私人文件夹中的图像?
发布评论