简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM