簡體   English   中英

Rails 3.1-對蝦使用Devise / Cancan

[英]Rails 3.1 - Using Devise/Cancan with Prawn

在我的html.erb視圖中,我通常可以使用如下代碼:

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

我正在嘗試在基於Prawn的PDF中執行相同的操作,但收到“未定義的局部變量或方法'current_user'”錯誤。

我了解我需要在我的PDF類中明確包含一個幫助程序,例如:

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

但這似乎不起作用嗎? 關於正確包含或其他方法的任何想法?

提前致謝!

編輯:我認為current_user實際上是我需要訪問的Devise方法。

好吧, current_user確實是一種Devise方法,並且在視圖,幫助器和控制器中可用,但在模型中不可用。 更好的方法是檢查用戶在控制器中的角色,然后調用特定的Prawn文檔。

除非每個用戶都需要截然不同的報告,否則您可以將view_context作為參數之一。

控制者

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

然后在MyPdf中,您可以選擇...

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

....

因此,現在您可以在MyPdf中的所有方法(以及其他視圖上下文幫助器)中使用它了……

@view.current_user

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM