我安装了 wicked PDF 并修改了我的控制器:
def show
respond_to do |format|
format.pdf do
render :pdf => "file_name"
end
format.html
end
end
这是我链接到 pdf 的方式:
compte_contrat_path(c,:format=>'pdf')
它适用于 html(没有格式),但不适用于 PDF,并出现以下错误:
模板丢失
缺少模板合同/显示 {:locale=>[:fr], :formats=>[:pdf], :handlers=>[:erb, :builder, :coffee, :arb]}。搜索位置:* “/home/sylario/ruby/place_de_marche/app/views”* “/usr/local/rvm/gems/ruby-1.9.2-p136/gems/activeadmin-0.5.0/app/views” *“/usr/local/rvm/gems/ruby-1.9.2-p136/gems/kaminari-0.14.1/app/views”*“/usr/local/rvm/gems/ruby-1.9.2-p136/ gems/devise-2.2.0/app/views"
我做错了什么?
多亏了henry,我现在知道这与ERB的格式有关。 我找到了一种重用 html.erb 文件的方法:
首先我在控制器中执行以下操作
format.pdf do
render :pdf => "file.pdf", :template => 'contrats/show.html.erb'
end
然后,当我使用部分时,我这样称呼它们:
render :partial => 'fullpath/toview.html.erb', :formats => [:html], :locals => { :mylocal=>@something }
对于 Rails 7,这是可行的:
format.pdf do
render pdf: "file_name", template: "posts/show", formats: [:html]
end
请注意,模板不再具有 .html.erb 并且不要忘记包含“格式”。
如果您从 Rails 6 移动到 Rails 7,那么您需要删除针对任何属性(包括布局)定义的
.html.erb
。
render pdf: "receipt", template: 'pdf/receipt.html.erb', layout: "pdf.html.erb", formats: [:html]
render pdf: "receipt", template: 'pdf/receipt', layout: "pdf", formats: [:html]