创建动态图像的Ruby on 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'
答案 0 :(得分:1)
对于制作,您必须将其写入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的主要资产文件夹是公开的。 app/assets
和vendor/assets
中的所有资产都汇总到public/assets