RoR:推送到生产环境,无法访问图像

RoR: Pushing to production and can't access image

Ruby 在 rails 上创建动态图像的应用仅在开发中显示,在生产中不显示。这是控制器:

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

视图如下:

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

现在它可以用于开发,但不能用于生产。

生产 HTML:

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

发展HTML

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

尝试将 file_name 更改为:

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

并收到此错误:

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'

对于制作,您必须将其写入 public 文件夹。

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

在生产环境中,Rails 的主要资产文件夹是 public。来自 app/assetsvendor/assets 的所有资产编译到 public/assets

中的生产