【发布时间】:2011-12-14 19:42:18
【问题描述】:
我正在使用 Prawn 从我的控制器生成 PDF,当直接通过 url 访问时,它可以完美运行,即 I.E.本地主机:3000/responses/1.pdf
但是,当我尝试动态生成此文件以包含在 Mailer 中时,一切都冻结并超时。
我尝试了各种生成/附加文件的方法,但都没有改变结果。
我也尝试过修改 Net::HTTP 的超时时间,但无济于事,超时时间更长。
如果我在 Rails 控制台上运行此命令,我会收到一个 PDF 数据流。
Net::HTTP.get('127.0.0.1',"/responses/1.pdf", 3000)
但如果我在控制器中包含此代码,则会超时。
我尝试了两种不同的方法,但都反复失败。
方法一 控制器:
http = Net::HTTP.new('localhost', 3000)
http.read_timeout = 6000
file = http.get(response_path(@response, :format => 'pdf')) #timeout here
ResponseMailer.confirmComplete(@response,file).deliver #deliver the mail!
方法 1 邮件:
def confirmComplete(response,file)
email_address = response.supervisor_id
attachments["test.pdf"] = {:mime_type => "application/pdf", :content=> file}
mail to: email_address, subject: 'Thank you for your feedback!'
end
以上代码超时。
方法 2 控制器:
ResponseMailer.confirmComplete(@response).deliver #deliver the mail!
方法 2 邮件:
def confirmComplete(response)
email_address = response.supervisor_id
attachment "application/pdf" do |a|
a.body = Net::HTTP.get('127.0.0.1',"/responses/1.pdf", 3000) #timeout here
a.filename = "test.pdf"
end
mail to: email_address, subject: 'Thank you for your feedback!'
结束
如果我切换 a.body 和 a.filename,它首先会出错
undefined method `filename=' for #<Mail::Part:0x007ff620e05678>
我找到的每个示例都有不同的语法或建议,但没有一个可以解决 Net::HTTP 超时的问题。 Rails 3.1、Ruby 1.9.2
【问题讨论】:
-
您是否尝试使用
curl或wget获取该pdf url? -
Wget 也卡住了
Resolving localhost... 127.0.0.1 Connecting to localhost|127.0.0.1|:3000... connected. HTTP request sent, awaiting response...并挂在那里。不过,同样的命令也可以在命令行上运行。 -
Web Server 似乎有奇怪的问题,尝试使用另一个,f.ex。如果您目前使用 WEBrick,请尝试与 Mongrel 或Passenger 确认
标签: ruby pdf ruby-on-rails-3.1 prawn net-http