Search code examples
ruby-on-railsimagermagickgruff

RoR: Pushing to production and can't access image


Ruby on rails app that creates a dynamic image only shows on development but not on production. Here is the controller:

def show
  require 'gruff'
  g = Gruff::Pie.new
  g.title = "Percentages of Fees"
  g.data 'Management Fees', mf_percent
  g.data 'Other Fees', of_percent
  file_name = 'app/assets/images/pie_charts/portfolio_' + @portfolio.id.to_s + '.png'
  g.write(file_name)
end

And here is the view:

<%= file_name = 'pie_charts/portfolio_' + @portfolio.id.to_s + '.png' %>
<%= image_tag(file_name, height: 300 ) %>

Now it works on development, but not in production.

Production HTML:

pie_charts/portfolio_3.png
<img height="300" src="/images/pie_charts/portfolio_3.png" alt="Portfolio 3">

Development HTML

pie_charts/portfolio_3.png
<img height="300" src="/assets/pie_charts/portfolio_3-8ea1d8e07ae40e233bd7366c24bc657795b6fba09095335ca0211cef72a07d3a.png" alt="Portfolio 3 8ea1d8e07ae40e233bd7366c24bc657795b6fba09095335ca0211cef72a07d3a">

Tried changing file_name to:

file_name = "public/images/pie_charts/portfolio_" + @portfolio.id.to_s + '.png'

and got this error:

F, [2015-08-29T02:09:19.026907 #92497] FATAL -- : 
Magick::ImageMagickError (WriteBlob Failed `public/images/pie_charts/portfolio_3.png' @ error/png.c/MagickPNGErrorHandler/1630):
app/controllers/portfolios_controller.rb:42:in `show'

Solution

  • For the production you must write it to the public folder.

    def show
      require 'gruff'
      g = Gruff::Pie.new
      g.title = "Percentages of Fees"
      g.data 'Management Fees', mf_percent
      g.data 'Other Fees', of_percent
      file_name = "#{Rails.root}/public/images/pie_charts/portfolio_" + @portfolio.id.to_s + '.png'
      g.write(file_name)
    end
    

    In production environment the main asset folder for Rails it's public. All assets from app/assets and vendor/assets compiled to the production placed in public/assets