1
votes

Puzzled by this error:

NameError in ProductionController#weekly_schedule
uninitialized constant ProductionController::WeeklySchedulePdf

I'm using prawn_rails gem, with the following code from the tutorial at http://www.sitepoint.com/pdf-generation-rails/.

Here's my controller snippet...

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

And my Prawn file, in 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

2 Answers

3
votes

It turns out all I had to do (using rails 4.2) was rename the file from weekly_schedule.rb to weekly_schedule_pdf.rb

0
votes

Source files in app/pdf aren't automatically discovered by Rails (they used to). You need to add the path into config.autoload_paths to make the class available.

# config/application.rb
config.autoload_paths += %W(#{config.root}/app/pdf)

http://guides.rubyonrails.org/configuring.html#configuring-rails-components