-1

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

Ilya Konyukhov
  • 2,666
  • 1
  • 12
  • 21
Gerson
  • 3
  • 3
  • Hi, What I want is to generate a pdf with the objects resulting from the query; in this case it would be @questions – Gerson Nov 01 '18 at 17:14
  • When downloading the pdf, the query data is lost. – Gerson Nov 01 '18 at 17:22
  • When I go to the URL of the pdf, http: // localhost: 3000 / simulators.pdf, the query data was lost – Gerson Nov 01 '18 at 17:28
  • ´ 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) ´ – Gerson Nov 01 '18 at 17:40
  • That's what the console shows, the query is lost. ok i'm going to edit the question – Gerson Nov 01 '18 at 17:43
  • You might as well also show (edit question, not in comments) the code where you generate the url. – jvillian Nov 01 '18 at 17:45
  • You know if there is another way to pass "@question" to the pdf template, Another doubt, why the query data is lost, in theory should generate the pdf without problems. – Gerson Nov 01 '18 at 18:01

1 Answers1

0

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.

xploshioOn
  • 4,035
  • 2
  • 28
  • 37
  • Hi, It is true what is mentioned, however, the data of the query is lost. It seems to rerun the query by returning other data, how could this be prevented – Gerson Nov 01 '18 at 19:55