ruby-on-railspdfwicked-pdfrespond-to

Can't generate pdf with wicked_pdf in rails ,using search results


Hi everybody I'm trying to use wicked pdf in rails. Get a pdf based on the object generated by the query, @questions

When downloading the pdf, the query data is lost. when I visit "/simulators.pdf" it shows me the following error in console

Started GET "/simulators.pdf" for 127.0.0.1 at 2018-11-01 12:38:19 -0500
Processing by SimulatorsController#index as PDF
Test Load (0.2ms)  SELECT  "tests".* FROM "tests" WHERE "tests"."name" IS NULL LIMIT $1  [["LIMIT", 1]]
CACHE Test Load (0.0ms)  SELECT  "tests".* FROM "tests" WHERE "tests"."name" IS NULL LIMIT $1  [["LIMIT", 1]]
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.2ms)

NoMethodError (undefined method `id' for nil:NilClass):

Controller:

def index
  @test = Test.find_by(name: params[:test])
  @questions = TestQuestion.joins(:test).where(test_id: @test.id).order('RANDOM()').limit(10)
  respond_to do |format|
    format.html
    format.pdf { render template: 'simulators/pdf', pdf: 'pdf'}
  end
end

URL:

http://localhost:3000/simulators?test=biology

This is url of index view

Can you help me? Many thanks


Solution

  • that's because when you ask for the pdf, you need to send the query in the url too

     http://localhost:3000/simulators.pdf?test=biology
    

    if you don't send the variable in the url, the search that you are doing will be nil, that's the error you are having.