创建一个图像,然后在页面中显示不在rails上的ruby中工作

时间:2013-05-15 08:26:34

标签: ruby-on-rails ruby gruff

我使用“Gruff”为我的数据库表格绘制图表

此功能的最后一步是在文件夹

中创建图像
bar_chart.write("/home/lawyer/Capistrano/current/app/assets/images/graph/lawfirm_#{@lawyer.id}.png")

并在展示页面中:它将使用此图片显示在某个div

-if File.exists?("/home/lawyer/Capistrano/current/app/assets/images/graph/lawfirm_#    {@lawyer.id}.png")
= image_tag("graph/lawfirm_#{@lawyer.id}.png") 

它在我的本地主机上运行良好。但在生产中,图像始终不显示。

在控制台日志中显示错误消息:

Started GET "/assets/graph/lawfirm_2673.png" for 217.86.186.128 at 2013-05-15 10:19:04 +0200
Served asset /graph/lawfirm_2673.png - 404 Not Found (3ms)

ActionController::RoutingError (No route matches [GET] "/assets/graph/lawfirm_2673.png"):

有时它有效。它可以显示图像。

首先绘制图形的功能在同一控制器和同一方法“索引”中,我希望先绘制图形然后显示图像。所以我添加

before_filter :draw_graph

并在方法“draw_graph”

中移动绘制图形函数

但仍然是同样的问题

2 个答案:

答案 0 :(得分:0)

我认为这可能是因为您在应用中生成了图片?如果您在应用程序中生成它们然后存储它们,那么如果您部署到像Heroku这样的服务,它们将无法在生产中使用。您需要使用Amazon S3之类的内容来保存您的图片。

This post描述了类似的问题。

有一个宝石可以安装AWS和像this这样的体面教程(使用paperclip进行设置,但适用相同的原则)。

答案 1 :(得分:0)

您正在将图片写入app / assets;默认情况下,Rails不会在生产中提供资产(请参阅config.assets.compile中的config/environments/production.rb)。

尝试将图像写入

bar_chart.write(File.join(Rails.root, "public", "system", "graph", "lawfirm_#{@lawyer.id}.png"))

并将您的image_tag更改为

image_tag("/system/graph/lawfirm_#{@lawyer.id}.png")

这会将动态图像写入public/system/文件夹。