render_to_string 未找到部分(PDFKit 控制器响应) [英] render_to_string does not find partials (PDFKit controller response)

查看:55
本文介绍了render_to_string 未找到部分(PDFKit 控制器响应)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby 1.8.7、Rails 3.0.4、PDFKit 0.5.0

Ruby 1.8.7, Rails 3.0.4, PDFKit 0.5.0

我试图在不使用中间件的情况下使用 PDFKit 创建 PDF,因此我可以禁用 javascript(那里有一个手风琴操作,它隐藏了很多应该在 PDF 上的信息).但是,每当我尝试时,它都会失败,因为它说我的视图中的部分 (show.html.erb) 丢失了:

I'm trying to create a PDF with PDFKit without using the middleware so I can disable javascript (there's an accordion action in there that hides a lot of info that should be on the PDF). However, whenever I try, it fails because it says the partials in my view (show.html.erb) are missing:

缺少带有 {:locale=>[:en, :en], :formats=>[:pdf], :handlers=>[:erb, :rjs, :builder, :rhtml, :rxml 的部分程序/细节]}

Missing partial programs/details with {:locale=>[:en, :en], :formats=>[:pdf], :handlers=>[:erb, :rjs, :builder, :rhtml, :rxml]}

如果我删除对部分的引用,它工作正常.我也尝试将部分与 show.html.erb 放在同一目录中,但无济于事.这是我的控制器显示操作中的代码:

If I remove the references to the partials, it works fine. I've also tried putting the partials in the same directory with show.html.erb to no avail. Here is the code in my controller's show action:

respond_to do |format| 
  format.html # show.html.erb 
  format.pdf {
    html = render_to_string(:template => "show.html.erb")
    kit = PDFKit.new(html, :disable_javascript => true )
    send_data(kit.to_pdf, :filename => "test_pdf", :type => "application/pdf", :disposition => 'attachment')
  }
end

有没有办法做到这一点并保留部分?

Is there any way to do this and keep the partials?

现在我已经做到了:

# config/initializers/pdfkit.rb
PDFKit.configure do |config|
  config.default_options = {
    :page_size => 'Legal',
    :print_media_type => true,
    :disable_javascript => true
  }
end

这样做的缺点是为我生成的每个 PDF 都关闭了 javascript,但现在可以了.任何关于让部分仍然与 render_to_string 一起工作的原始问题的答案仍然受到赞赏.

This has the disadvantage of turning off javascript for every PDF I generate, but it'll do for now. Any answers on the original question of getting the partials to work still with render_to_string still appreciated.

推荐答案

今天早上我遇到了这个问题,在寻找解决方案时遇到了你的问题.

I was running into this issue this morning and came across your question while looking for a solution.

控制器摘录:

respond_to do |format|
  format.html
  format.pdf {
    html = render_to_string(:layout => false , :action => "constitution.pdf.haml")
    kit = PDFKit.new(html)
    kit.stylesheets << "#{Rails.root}/public/stylesheets/pdf.css"
    send_data(kit.to_pdf, :filename => "#{@organisation_name} Constitution.pdf",
      :type => 'application/pdf', :disposition => 'inline')        
    return
  }
end

constitution.pdf.haml 摘录:

=render :partial => 'shared/constitution'

错误:

Missing partial shared/constitution with {:locale=>[:en, :e   ...

我的头撞墙了一段时间后,我有了一个猜测并将constitution.pdf.haml更改为:

After a while banging my head against a wall, I had a guess and changed constitution.pdf.haml to:

=render :partial => 'shared/constitution.html.haml'

我对 Rails 知之甚少.难道真的是这样(与我普通的 Haml 视图不同),PDFKit 需要文件扩展名吗?这已经为我修好了!

I only know a tiny amount about Rails. Could it really be that (unlike my normal Haml views), PDFKit requires the file extension? That's fixed it for me!

这篇关于render_to_string 未找到部分(PDFKit 控制器响应)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆