Rails:显示数据库中blob字段的图像 [英] Rails: displaying an image from a blob field in a database
问题描述
所以,我设法将一个图像blob放到我的MySQL数据库中(该字段中有一个大的十六进制数字),但我找不到任何关于如何在rails环境中显示图像的文档...当它打印出来,它以GIF89开始...然后当你在记事本中打开它时,你在GIF中看到的gobbledygook字符。 :P非常感谢任何线索!
So, I managed to get an image blob into my MySQL database (there's a big hex number in the field), but I can't find any documentation on how to display the image in a rails environment... when it prints out, it starts with a GIF89... and then the gobbledygook characters you see in a GIF when you open it in Notepad. : P Any clues would be greatly appreciated!
谢谢。
推荐答案
以下代码应该有效。在你的控制器中,创建一个方法:
The following code should work. In your controller, create a method:
def show_image
@user = User.find(params[:id])
send_data @user.image, :type => 'image/png',:disposition => 'inline'
end
在您的视图中:
<%= image_tag url_for(:controller => "mycontroller", :action => "show_image", :id => @user.id) %>
我建议使用Paperclip gem。它使得保存/查看图像变得非常容易。
I would recommend using the Paperclip gem. It makes saving/viewing of images really easy.
这篇关于Rails:显示数据库中blob字段的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!