title: Create PDF with wickedpdf and save with paperclip
body: <p>sorry about my english, its not my native language.</p>
  
  <p>I need to create a PDF file (I can render it right now) , and then save it using paperclip (already installed and working with other models)</p>
  
  <p>I found this solution :</p>
  
  <p><a href="https://github.com/thoughtbot/paperclip/wiki/PDF-Email-Attachments-with-Paperclip-and-wicked_pdf" rel="nofollow">https://github.com/thoughtbot/paperclip/wiki/PDF-Email-Attachments-with-Paperclip-and-wicked_pdf</a></p>
  
  <p>But it uses an already created report and I need to create this PDF everytime I create a new record, I'm getting "no method for nill class so probably thats why is not working for me.</p>
  
  <p>I tried adding this method inside the "if @instrument.save" but didnt work.</p>
  
  <p>Let me show you my code :</p>
  
  <p>instrument.erb</p>
  
  <pre><code>class Instrument &lt; ActiveRecord::Base
  
    belongs_to :evaluation
    has_attached_file :instrument_document
    validates_attachment :instrument_document, content_type: { content_type: "application/pdf" }
    do_not_validate_attachment_file_type :instrument_document
  
  end
  </code></pre>
  
  <p>instruments#show</p>
  
  <pre><code> def show
      respond_to do |format|
        format.html { redirect_to instruments_url }
        format.pdf do
          render  :pdf =&gt; "file.pdf", :template =&gt; 'instruments/instrument_document.html.erb', :margin =&gt; {:top =&gt; 15, :bottom =&gt;15 },:header =&gt; {   html: {            template: 'instruments/header.pdf.erb', }}, :footer =&gt; { right: '[page]' }
        end 
      end
    end
  </code></pre>
  
  <p>instruments#create</p>
  
  <pre><code>  def create
      @instrument = Instrument.new(instrument_params)
  
      respond_to do |format|
        if @instrument.save
  
          # 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_html = av.render template: 'instruments/instrument_document.html.erb'
  
          # use wicked_pdf gem to create PDF from the doc HTML
          doc_pdf = WickedPdf.new.pdf_from_string(
            pdf_html,
            page_size: 'Letter',
            javascript_delay: 6000
            )
  
          # save PDF to disk
          pdf_path = Rails.root.join('tmp/reports', "#{@instrument.id}_#{Date.today.iso8601}.pdf")
          File.open(pdf_path, 'wb') do |file|
            file &lt;&lt; doc_pdf
          end
  
          @instrument.instrument_document = File.open pdf_path
          @instrument.save!
  
          # The report has now been saved elsewhere using Paperclip; we don't need to store it locally
          File.delete(pdf_path) if File.exist?(pdf_path)
  
  
          format.html { redirect_to @instrument, notice: 'Instrumento creado con éxito.' }
          format.json { render :show, status: :created, location: @instrument }
        else
          format.html { render :new }
          format.json { render json: @instrument.errors, status: :unprocessable_entity }
        end
      end
    end
  </code></pre>
  
  <p>I'm getting : </p>
  
  <p>undefined method `evaluation' for nil:NilClass</p>
  
  <p>Thanks!</p>