微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – render_to_string没有找到partials(PDFKit控制器响应)

Ruby 1.8.7,Rails 3.0.4,PDFKit 0.5.0

我正在尝试使用PDFKit创建一个PDF,而不使用中间件,所以我可以禁用javascript(在那里隐藏了大量应该在PDF上的信息的手风琴动作).但是,每当我尝试,它失败,因为它说我的视图(show.html.erb)中的部分丢失:

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

如果我删除对部分的引用,它可以正常工作.我也尝试将parts放在同一个目录中,并且show.html.erb无效.这是我的控制器的显示操作中的代码

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

有什么办法可以做到这一点吗?

编辑:现在我做到了:

# 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的原始问题的任何答案仍然赞赏.

解决方法

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

控制器提取

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

宪法.pdf.haml提取物:

=render :partial => 'shared/constitution'

错误

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

过了一会儿,我的头撞在墙上,我有一个猜测,并改变了宪法.pdf.haml:

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

我只知道一些关于Rails的数量.真的可以吗(不像我正常的哈姆意见),PDFKit需要文件扩展名?这是为我修复的!

原文地址:https://www.jb51.cc/ruby/272192.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐