[英]Can't generate pdf with wicked_pdf in rails ,using search results
大家好,我正在嘗試在 Rails 中使用 wicked pdf。 根據查詢生成的對象獲取pdf,@questions
下載pdf時,查詢數據丟失。 當我訪問“/simulators.pdf”時,它會在控制台中顯示以下錯誤
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):
控制器:
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
網址:
http://localhost:3000/simulators?test=biology
這是索引視圖的 url
你能幫助我嗎? 非常感謝
那是因為當您要求 pdf 時,您也需要在 url 中發送查詢
http://localhost:3000/simulators.pdf?test=biology
如果您不在 url 中發送變量,則您正在執行的搜索將為零,這就是您遇到的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.