Rails PDFKit 仅在 Heroku 上产生 ActionController:UnknownFormat

Rails PDFKit yields ActionController:UnknownFormat only on Heroku

所以我正在尝试开发一个应用程序,让您可以将产品转换为带有二维码的标签。使用 gem PDFKit 和 wkhtmltopdf 它在本地工作正常。但是在 Heroku 上,它给我一个 HTTP 406 和以下 rails 错误:ActionController::UnknownFormat (OrderController#print is missing a template for this request format and variant. request.formats: ["application/pdf"] request.variants: [])。我搜索了论坛,但似乎找不到遇到同样问题的人。我还遵循了 PDFKit github 中的 heroku 使用指南 link to guide。意思是我同时拥有 wkhtmltopdf-binary gem 和 wkhtmltopdf-heroku gem。

相关代码片段:

#in app/views/order/show.html.erb
<%= link_to "Print QR-code", print_order_path(@order, :format => :pdf), class 'btn btn-primary' %>



#in app/config/initializers/pdfkit.rb
PDFKit.configure do  |config|
if File.executable? 'app/.apt/usr/local/bin/wkhtmltopdf'
   config.wkhtmltopdf = 'app/.apt/usr/local/bin/wkhtmltopdf'
end


#app/controllers/order_controller
def print
@order = Order.find(params[:id])
end

#app/views/order/print.html.erb
<div style="display: inline-block">
   <table class="QR" style="width:100%">
       <tr style="margin:30px">
           <td class="QR">
               <% @qr = RQRCode::QRCode.new(@order.qr_code.to_s, :size => 6)%>
               <%= raw @qr.as_svg(offset:0, color: '000',shape_rendering: 'crispEdges', module_size:4) %>
           </td>
           <td style="font-size:50px;padding:20px; text-align:center">
               <%=@order.garment.to_s%><br>
               <%=@order.size.to_s%>
           </td>
        </tr>
   </table>
</div>

使用 srng 提供的 respond_to 添加块后,我现在收到以下错误:

ActionView::MissingTemplate(Missing template order/print, application/print with {:locale =>[:sv], :formats =>[:pdf], :variants => [], :handlers => [:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}

我相信您缺少格式块。这就是它要找的东西。

您需要在 Order.rb #print 方法中添加一个块;

// order_controller.rb 
def print
    @order = Order.find(params[:id])
    respond_to do |format|
        # format.html
  format.pdf do
    send_data PDFKit.new( render_to_string 'print', order: @order ).to_pdf
        end
    end
end

编辑:天哪,太有趣了。

1- 将名为 print.pdf.erb 的文件添加到您的 views/order 文件夹中 2- 内容与 print.html.erb 相同 2- 使用上面的格式块,我已经将 html 格式注释掉了,因为不需要

EDIT2:鉴于它在本地和我这边的 Heroku 上工作,我将copy/paste 这里的所有代码进行直接比较。我没有二维码class,我的属性也不一样,不过你可以理解一下。

//orders_controller.rb
def print
    @order = Order.find(params[:order_id])
    respond_to do |format|
      format.html
      format.pdf do
        send_data PDFKit.new( render_to_string 'print', order: @order ).to_pdf
      end
    end
  end

  # GET /orders
  # GET /orders.json
  def index
    @orders = Order.all
  end

  # GET /orders/1
  # GET /orders/1.json
  def show
  end
....

//views/orders/show.html.erb
<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @order.name %>
</p>

<p>
  <strong>Thing:</strong>
  <%= @order.thing %>
</p>
<%= link_to 'Print', order_print_path(@order, :format => :pdf) %> |

<%= link_to 'Edit', edit_order_path(@order) %> |
<%= link_to 'Back', orders_path %>

//views/order/print.html.erb
<div style="display: inline-block">
  <table class="QR" style="width:100%">
    <tr style="margin:30px">
      <td class="QR">

      </td>
      <td style="font-size:50px;padding:20px; text-align:center">
        <%=@order.name%><br>
        <%=@order.thing%>
      </td>
    </tr>
  </table>
</div>

//views/orders/print.pdf.erb
<div style="display: inline-block">
  <table class="QR" style="width:100%">
    <tr style="margin:30px">
      <td class="QR">

      </td>
      <td style="font-size:50px;padding:20px; text-align:center">
        <%=@order.name%><br>
        <%=@order.thing%>
      </td>
    </tr>
  </table>
</div>

//Gemfile
gem 'pdfkit'
gem 'wkhtmltopdf-heroku'

//config/routes.rb
resources :orders do
    get 'print', to: 'orders#print'
  end