Wicked_PDF 未呈现正确的模板

Wicked_PDF not rendering correct template

您好,我有一份撰写提案的申请。我正在尝试设置 wicked_pdf 来生成 pdf,但出于某种原因,它没有使用正确的模板。它使用我的应用程序模板,而不是我的 pdf 模板。

app/controllers/proposals_controller.rb

def show
    @proposal = Proposal.find(params[:id])
    @custom_content = @proposal.custom_contents
    respond_to do |format|
      format.html
      format.pdf do
        render pdf: "some_name",
               template: "proposals/show.pdf.erb",
               layout: "pdf.html",
               margin: { top: 35, bottom: 35 }
      end
    end
  end

app/views/layouts/pdf.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>PDF</title>
  <%= wicked_pdf_stylesheet_link_tag "application" -%>
</head>
<body>
<div class='container d-flex justify-content-center'>
  test 2
  <%= yield %>
</div>
</body>
</html>

app/views/proposals/show.pdf.erb

<div class="row full-page justify-content-center" id="cover">
  <div class="container">
    <div class="row p-0">
      <div class="col-12 d-flex align-items-end ">
        <%= wicked_pdf_image_tag '1209232_1920-1.jpg' %>
      </div>
    </div>
    <div class="row pt-1 d-flex align-items-end text-justify">
      <div class="col-12 text-justify cover-background">
        <div class="row pt-1">
          <h2 class="text-white text-justify">Proposal For <br> Maintenance</h2>
        </div>
        <div class="row p-0">
          <div class="col-6">
            <h5 class="text-white text-justify">Prepared For: <%= @proposal.contact.name %></h5>
            <h6></h6>
          </div>
          <div class="col-6">
            <h5 class="text-white text-justify">Prepared By: <%= @proposal.user.name %></h5>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

config/initializers/wicked_pdf.rb

# WickedPDF Global Configuration
#
# Use this to set up shared configuration options for your entire application.
# Any of the configuration options shown here can also be applied to single
# models by passing arguments to the `render :pdf` call.
#
# To learn more, check out the README:
#
# https://github.com/mileszs/wicked_pdf/blob/master/README.md

WickedPdf.config = {
    }

config/initializers/mime_types.rb

# Be sure to restart your server when you modify this file.

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
Mime::Type.register "application/pdf", :pdf

我觉得我已经看了这么久,我可能错过了显而易见的东西。我什至 运行 准备了一个示例应用程序来测试...并且没有遇到任何问题。

我跟着这个很棒的教程 - https://medium.com/@yazed.jamal/using-wicked-pdf-with-rails-5-3d1a4b0a09ba

非常感谢任何帮助

另外要注意的是创建的文件不是"some_name"而是id .pdf

我的按钮位于我的 app/views/proposals/_proposal-cover。html.erb 这是我的 app/views/proposals/show.html.erb

的一部分
<%= link_to 'Download', proposal_path(@proposal, :format => :pdf), class: 'btn btn-default'  %>

试试下面的方法。我在我的应用程序中使用了 wicked,这个结构对我有用,希望对你有用。

显示

<%= link_to 'Download', proposal_path(format: 'pdf'), class: 'btn btn-default'  %>

控制器

format.pdf do
 render pdf: "some_name",
  template: "proposals/show",
  margin: { top: 35, bottom: 35 }
end

我想感谢@dollarchills 和@nourza。两者都对这一个有很大帮助。

原来是其他开发者安装了以下

If you would like to have WickedPdf automatically generate PDF views for all (or nearly all) pages by appending .pdf to the URL, add the following to your Rails app:

在 application.rb (Rails3) 或 environment.rb (Rails2)

require 'wicked_pdf'
config.middleware.use WickedPdf::Middleware

这会覆盖其他所有内容