我需要从rake任务创建一个pdf。因此我从模型本身生成pdf。 我使用wicket_pdf并在网上搜索了很多,如何创建pdf文件。 在浏览器中,pdf可以工作(简单的一部分; - ))。
我的环境:
到目前为止,这是我的解决方案:
# app/controllers/bills_controller.rb
class BillsController < ApplicationController
before_filter :authenticate_admin_user!
respond_to :html, :pdf
def show
@bill = Bill.find(params[:id])
respond_with(@bill) do |format|
format.html
format.pdf { render :pdf => "mypdf", :disposition => "inline" }
end
end
end
# app/models/bill.rb
# solution from https://github.com/mileszs/wicked_pdf/issues/84
class Bill < ActiveRecord::Base
require 'open-uri'
def save_pdf
pdf = open("http://localhost:3000/bills/#{id}.pdf")
File.open(Rails.root.join("pdfs","#{id}.pdf"), 'wb') { |f| f.print(pdf.read) }
end
end
来自控制台:
Bill.first.save_pdf
但我得到错误:
OpenURI::HTTPError: 401 Unauthorized
如何通过rake任务中的open-uri对devise_user进行身份验证?