Ruby NameError,未初始化的常量,使用Prawn

时间:2015-05-08 20:32:51

标签: ruby-on-rails ruby 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

2 个答案:

答案 0 :(得分:3)

事实证明我所做的一切(使用rails 4.2)将文件从weekly_schedule.rb重命名为weekly_schedule_pdf.rb

答案 1 :(得分:0)

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