Rails PDFKit仅在Heroku上产生ActionController:UnknownFormat

问题描述 投票:1回答:1

因此,我试图开发一个应用程序,让您将产品转换为带有QR码的标签。使用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">
               <%[email protected]_s%><br>
               <%[email protected]_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]}
ruby-on-rails heroku wkhtmltopdf pdfkit
1个回答
0
投票

你错过了我认为的格式块。这就是它正在寻找的东西。

您需要在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格式注释为不必要的

编辑2:看到它在本地和Heroku上工作,我只是要复制/粘贴所有代码,以便直接比较。我没有qr类,我的属性不同,但你可以得到这个想法。

//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">
        <%[email protected]%><br>
        <%[email protected]%>
      </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">
        <%[email protected]%><br>
        <%[email protected]%>
      </td>
    </tr>
  </table>
</div>

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

//config/routes.rb
resources :orders do
    get 'print', to: 'orders#print'
  end
© www.soinside.com 2019 - 2024. All rights reserved.