2014-09-07 2 views
3

WickedPDF gem과 Sidekiq를 사용하여 PDF를 인쇄해야합니다.Sidekq 직원의 Wicked PDF 생성 : 부분 전달 방법?

def print_token 
    @token = Token.find(params[:id]) 
    TokenPdfPrinter.perform_async(@token.id) 
    redirect_to :back 
end 

그리고 노동자 : 나는/생각이 거의 작동 희망, 여기에 관련 컨트롤러 코드

class TokenPdfPrinter 
    include Sidekiq::Worker 
    sidekiq_options retry: false 

    def perform(token_id) 
    @token = Token.find(token_id) 
    # create an instance of ActionView, so we can use the render method outside of a controller 
    av = ActionView::Base.new() 
    av.view_paths = ActionController::Base.view_paths 

    # need these in case your view constructs any links or references any helper methods. 
    av.class_eval do 
     include Rails.application.routes.url_helpers 
     include ApplicationHelper 
    end 

    pdf = av.render pdf: "Token ##{ @token.id }", 
       file: "#{ Rails.root }/app/admin/pdfs/token_pdf.html.erb", 
      layout: 'layouts/codes', 
     page_height: '3.5in', 
     page_width: '2in', 
      margin: { top: 2, 
         bottom: 2, 
          left: 3, 
          right: 3 }, 
      disposition: 'attachment', 
    disable_javascript: true, 
     enable_plugins: false, 
     locals: { token: @token } 

    send_data(pdf, filename: "test.pdf", type: "application/pdf") 
    end 
end 

그리고 파셜 렌더링하기 :

<!DOCTYPE html> 
<html> 
    <head> 
     <title>WuDii</title>  
     <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
     <%= wicked_pdf_stylesheet_link_tag "application" %> 
     <%= wicked_pdf_stylesheet_link_tag "codes" %> 
     <%= wicked_pdf_javascript_include_tag "application" %> 
    </head> 
    <body> 
     <%= yield %> 
    </body> 
    </html> 

그리고 token_pdf. html.erb :

<% @token = @token %> 
<br> 
<div class="barcode text-center"><br><br><br><br><br><br><br><br> 
    <p><%= @token.encrypted_token_code.scan(/.{4}|.+/).join('-') %></p> 
    <h3><strong>(<%= number_with_delimiter(@token.credits, delimiter: ',') %>)</strong></h3> 
</div> 

그리고 token_pdf.html.erb에서 @token이 nil이라는 오류가 발생합니다. 내가 여기서 뭐하고 있는거야?

답변

2

로컬 변수 토큰을보기 위해 전달하므로 @가없이 참조해야합니다. 첫 줄을 < % @token = token %>으로 변경하면 제대로 작동합니다. 또는 로컬 변수를 사용하여 첫 번째 줄과 참조 토큰을 건너 뜁니다.

관련 문제