[英]Ruby NameError, uninitialized constant, using Prawn
对这个错误感到困惑:
NameError in ProductionController#weekly_schedule
uninitialized constant ProductionController::WeeklySchedulePdf
我正在使用prawn_rails gem,以及http://www.sitepoint.com/pdf-generation-rails/上的教程中的以下代码。
这是我的控制器片段......
class ProductionController < ApplicationController
def weekly_schedule
@tickets = Ticket.where(active: true,
manufacturing_location: session[:factory]).
order(:calendar_date).order(:calendar_order)
if @tickets.size.zero?
@first_day, @last_day = Date.current, Date.current
else
@first_day = @tickets.minimum(:calendar_date)
@last_day = @tickets.maximum(:calendar_date)
#@dayspan = (@last_day - @first_day).to_i
end
vars = [@tickets, @first_day, @last_day]
respond_to do |format|
format.html
format.pdf do
pdf = WeeklySchedulePdf.new(vars)
send_data pdf.render, filename: 'weekly_schedule.pdf', type: 'application/pdf'
end
end
end
和我的Prawn文件,在app / pdf / weekly_schedule.rb中
class WeeklySchedulePdf < Prawn::Document
def initialize(vars)
super
@tickets = vars.first
@first_day = vars.second
@last_day = vars.third
#header
#text_content
#table_content
end
事实证明我所做的一切(使用rails 4.2)将文件从weekly_schedule.rb
重命名为weekly_schedule_pdf.rb
Rails(他们以前)不会自动发现app/pdf
中的源文件。 您需要将路径添加到config.autoload_paths
以使该类可用。
# config/application.rb
config.autoload_paths += %W(#{config.root}/app/pdf)
http://guides.rubyonrails.org/configuring.html#configuring-rails-components
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.