Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling classes in /lib from controller actions

hi i am a bit stuck with this. what i am going to work out is that i have a file called ticket_pdf.rb in lib/ directory which i am planning to generate some invoice PDFs for my app. I want to call a function of this class to generate the PDFs from my controller actions.

the ticket_pdf.rb looks like this

class TicketPDF
  def generate_pdf (purchase)
    puts "Ticket ID = #{purchase.ID}"
  end
end

in a controller I action i do this.

class Customer::MyController < ApplicationController
  require 'ticket_pdf'

  def show
    ticket = TicketPDF.new
  end
end

when i try to create an object like this it give me a 500 error like this one.

uninitialized constant Customer::MyController::TicketPDF

what i am doing wrong here ?

like image 635
nivanka Avatar asked Apr 08 '12 09:04

nivanka


1 Answers

Try

ticket = ::TicketPDF.new

You have created TicketPDF in the top level namespace.

like image 172
John Plummer Avatar answered Nov 06 '22 03:11

John Plummer