繁体   English   中英

无法使用搜索结果在 rails 中使用 wicked_pdf 生成 pdf

[英]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.

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