create a image then show in the page not working in ruby on rails

174 Views Asked by At

I use "Gruff" to draw a graph for my database table

the last step of this function is to create a image in the folder

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

and in the show page :it will use this image to show in a certain div

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

it works fine in my local host .but in production the image always not shown .

In the console log it shows the error message:

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"):

BUt sometimes it work. it can show the image.

At first the function of draw a graph is in the same controller and same method "index", I hope to draw the graph first then show the image. so I add

before_filter :draw_graph

and move the draw graph function in the method "draw_graph"

but still the same problem

2

There are 2 best solutions below

0
On

You're writing your image into app/assets; by default, Rails does not serve assets from there in production (see config.assets.compile in config/environments/production.rb).

Try instead writing your image to

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

and change your image_tag to

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

This will write your dynamic images into the public/system/ folder instead.

0
On

I think it could be because you're generating images in the app? If you are generating them within the app and then storing them, they will not work in production if you deploy to services like Heroku. You need to use something like Amazon S3 to hold your images in production.

This post describes a similar problem.

There's a gem to install AWS and decent tutorials like this (setup with paperclip, but the same principles apply).