0

In my html.erb views I can normally use code like:

if current_user.role?(:label)
"do something"
end

I'm trying to do the same in a Prawn based PDF but am getting a "undefined local variable or method `current_user'" error.

I understand I need to explicitly include a helper in my PDF class, something like:

class SalesnotePdf < Prawn::Document
  include CanCan::Ability
  "do pdf layout here"
end

But this doesn't appear to work? Any ideas on the correct include or other approach to this?

Thanks in advance!

EDIT: I think current_user is actually a Devise method I need to access.

Raoot
  • 1,751
  • 1
  • 25
  • 51

2 Answers2

1

Well, current_user really is a Devise method and is available in views, helpers and controllers, but not models. A better approach would be to check the user's role in a controller and then call a specific Prawn document.

0

Unless each user needs a significantly different report, you could pass view_context as one of your parameters.

Controller

...
format.pdf do
...
    pdf = MyPdf.new @records, reportDate, view_context
...
end

Then in MyPdf you pick that up...

def initialize(records, reportDate, view)
    super()
    @nonPagedTransactions = transactions
    @view = view

....

So now it is available to all your methods in MyPdf (as well as other view-context helpers), if you say...

@view.current_user
JosephK
  • 668
  • 10
  • 18