Rails4:PDFKit缺少模板错误(Rails4: Template is missing error with PDFKit)

我想生成PDF文件,所以我试图使用PDFKit但失败了。

单击链接时显示以下错误。

ActionView::MissingTemplate (Missing template /show with {:locale=>[:en], :formats=>[:pdf], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
  * "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates"
  * "/home/ubuntu/workspace/app/views"
  * "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/app/views"

时间表\ show.html.erb

<% provide(:title, @schedule.title) %>
<%= render @schedules %>

schedules \ _schedule.html.erb

...
    <%= link_to "PDF", schedule_path(schedule.id, format: "pdf"), class: "btn btn-sm btn-default" %>
...

schedules_controller.rb

...
respond_to do |format|
  format.html # show.html.erb
  format.pdf do
    html = render_to_string template: "show"

    pdf = PDFKit.new(html, encoding: "UTF-8")

    send_data pdf.to_pdf,
      filename:    "#{@scheudles.id}.pdf",
      type:        "application/pdf",
      disposition: "inline"
  end
end
...

虽然我创建了show.pdf.erb_schedule.pdf.erb ,其内容与_schedule.pdf.erb相同,但结果是一样的。


I'd like to generate PDF file, so I am trying to use PDFKit but failing.

The following error was displayed when I click the link.

ActionView::MissingTemplate (Missing template /show with {:locale=>[:en], :formats=>[:pdf], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
  * "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates"
  * "/home/ubuntu/workspace/app/views"
  * "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/app/views"

schedules\show.html.erb

<% provide(:title, @schedule.title) %>
<%= render @schedules %>

schedules\ _schedule.html.erb

...
    <%= link_to "PDF", schedule_path(schedule.id, format: "pdf"), class: "btn btn-sm btn-default" %>
...

schedules_controller.rb

...
respond_to do |format|
  format.html # show.html.erb
  format.pdf do
    html = render_to_string template: "show"

    pdf = PDFKit.new(html, encoding: "UTF-8")

    send_data pdf.to_pdf,
      filename:    "#{@scheudles.id}.pdf",
      type:        "application/pdf",
      disposition: "inline"
  end
end
...

Although I create show.pdf.erb and _schedule.pdf.erb which contents is the same as html.erb, the result is the same.


原文:https://stackoverflow.com/questions/39431808
2024-04-21 20:04

满意答案

您应该创建一个由servernamedatetime键入的字典

示例:from collections import defaultdict

lines = defaultdict(dict)
with open("file_parse.txt") as f:
    for line in f:
        parts = line.split()
        key = tuple(parts[1:3]) # unique key with servername and datetime
        lines[key][parts[0]] = parts[3]
        lines[key]["servername"] = parts[1]
        lines[key]["datetime"] = parts[3]
res = list(lines.values())
print(res)
# [
#    {'cpudile': 'NaN', 'cpunice': '0.0', 'cpunumber': '40.0', 'cpusystem': '0.0', 'cpuuser': '0.0', 'cpuwait': '0.0', 'datetime': 'NaN', 'servername': '1438084445'},
#    {'cpudile': 'NaN', 'cpunice': '0.0', 'cpunumber': '40.0', 'cpusystem': '0.0', 'cpuuser': '0.0', 'cpuwait': '0.0', 'datetime': 'NaN', 'servername': '1438084440'},
#    {'cpudile': 'NaN', 'cpunice': '0.0', 'cpunumber': '40.0', 'cpusystem': '0.0', 'cpuuser': '0.0', 'cpuwait': '0.0', 'datetime': 'NaN', 'servername': '1438084450'}
# ]

You should create a dictionary that is keyed by servername and datetime

Example: from collections import defaultdict

lines = defaultdict(dict)
with open("file_parse.txt") as f:
    for line in f:
        parts = line.split()
        key = tuple(parts[1:3]) # unique key with servername and datetime
        lines[key][parts[0]] = parts[3]
        lines[key]["servername"] = parts[1]
        lines[key]["datetime"] = parts[3]
res = list(lines.values())
print(res)
# [
#    {'cpudile': 'NaN', 'cpunice': '0.0', 'cpunumber': '40.0', 'cpusystem': '0.0', 'cpuuser': '0.0', 'cpuwait': '0.0', 'datetime': 'NaN', 'servername': '1438084445'},
#    {'cpudile': 'NaN', 'cpunice': '0.0', 'cpunumber': '40.0', 'cpusystem': '0.0', 'cpuuser': '0.0', 'cpuwait': '0.0', 'datetime': 'NaN', 'servername': '1438084440'},
#    {'cpudile': 'NaN', 'cpunice': '0.0', 'cpunumber': '40.0', 'cpusystem': '0.0', 'cpuuser': '0.0', 'cpuwait': '0.0', 'datetime': 'NaN', 'servername': '1438084450'}
# ]

相关问答

更多

在python写入新数据后,Logstash无法(读取和)存储文件中的日志数据(Logstash can't (read and) store log data from a file after python write new data into it)

从我收集的内容来看,源文件是elk_data_source.log,并且您正尝试将"eventType" = ["*icbc"]覆盖为*ABC 。 但是你错过的是...... "eventType"值是一个数组 ,你用一个单一的值来写它 - *ABC 。 将data["eventType"] = "*ABC"更改为data["eventType"] = ["*ABC"] 。 这应该解决。 如果可能,尝试将这两个文件与文件比较软件进行比较。 此外,请检查从文件读取和格式化时可能导致问题的匹配大括号或...

将MySQL表转储为CSV文件,并使用Python脚本将其保存到指定的位置(Dump a MySQL table to a CSV file and save it in a given location using Python script)

改变这个: with open('/full/path/tofile/Data on %s.csv' % currentDate ,'w') as f: 这解决了你的问题X.但是你有一个问题Y.那就是'我如何有效地从mysql转储CSV数据,而不必写很多代码?' 对问题Y的回答是SELECT INTO OUTFILE Change this: with open('/full/path/tofile/Data on %s.csv' % currentDate ,'w') as f: This ...

试图在我的一个Python脚本文件中存储原始字节(Trying to store raw bytes inside my one Python script file)

您应该对二进制文件进行编码 - 例如,使用base64封装 - 将字节转换为“合法字符”。 然后,当您需要二进制信息时,将其转换回来。 有关一些代码示例,请参阅此前一个问题 。 一个简短的示例,让您前进: # assume your bytes came from a file: bytesIneed = bytearray([234,232,231,188,122,132,145]) import base64 bytesConverted = base64.b64encode(bytesIn...

Python脚本从文件中读取数据并将其存储在mysql表中(Python script to read data from a file and store it in mysql table)

您应该创建一个由servername和datetime键入的字典 示例:from collections import defaultdict lines = defaultdict(dict) with open("file_parse.txt") as f: for line in f: parts = line.split() key = tuple(parts[1:3]) # unique key with servername and dateti...

如何从SQL数据库中读取数据并将其存储到XML文件中?(How to read data from SQL database and store it into an XML file?)

一种简单的方法(假设您使用的是SQL Server)是在检索数据的查询结尾附加FOR XML AUTO 。 然后这将返回结果集作为XML文件。 例如,以Northwind数据库为例,您可以使用此查询: SELECT * FROM Products as P INNER JOIN Categories as C ON P.CategoryID = C.CategoryID FOR XML AUTO 这将生成以下XML: <P ProductID="1" ProductName="Chai" ...

无法将mysql中的特殊字符存储到json文件中 - Python(unable to store special characters from mysql into a json file - Python)

您似乎正在使用json编码的字符串创建ASCII编码的json文件,这是存储JSON文件的典型用例。 我想你想要一个UTF-8编码的json文件。 为此,请在json编码步骤中设置ensure_ascii=False ,以便将utf8编码的字符串直接传递给文件。 这样的事可能适合你。 import json master_objects = { "tomorrow" : "ma\xc3\xb1ana" # UTF-8 encoding, just like what comes from ...

如何从XML文件中读取数据并将其存储到数据库(MySQL)中?(How to read data from an XML file and store it into database (MySQL)?)

您可以使用Castor witch是一个开源数据绑定框架,用于将数据从XML移动到Java编程语言对象以及从Java移动到数据库。 我在IBM developerWorks中找到了一篇文章系列,描述了使用适合您需求的Castor。 You could use Castor witch is an open source data binding framework for moving data from XML to Java programming language objects and f...

python:从文件读取行并将其存储在列表中(python: read line from file and store in list)

首先,您可能打算在列表中调用append ,如list_1.append(a[0])和list_2的相应更改。 此外,如果稍后将a[0]视为数字,则可能需要追加float(a[0]) 。 IndexError可能来自输入中的空行(可能是最后一行)。 要解决它,可以在访问其元素之前检查len(a) ,或者确保代码只有干净的输入。 First, you probably meant to call append on the lists, as in list_1.append(a[0]) and t...

相关文章

更多

Solr Document [null] missing required field: id 的原因

在solr建立索引的时候,如果你提交的doc中没有 id 这个Field,结果Solr在建立索引时候出 ...

Rails4 已经移出了params可以解析xml的功能,拿rails4做微信API的童鞋们注意了

拜读了用 Rails 搭建微信公众平台 API之后发现,params[:xml]这个办法在Rails ...

eclipse里报:An internal error occurred during:

eclipse里报:An internal error occurred during: Buildi ...

[Hadoop] Error: JAVA_HOME is not set

在namenode启动脚本%Hadoop_HOME%/bin/start-dfs.sh的时候发现dat ...

《数据结构与STL》(Data Structures and the Standard Template Library)扫描版[PDF]

中文名: 数据结构与STL 原名: Data Structures and the Standa ...

《Joomla 2.5 模板教程:宁皓网》(Create a Joomla 2.5 Template)前两章 + 模板资源[光盘镜像]

中文名: Joomla 2.5 模板教程:宁皓网 英文名: Create a Joomla 2. ...

The connection to adb is down, and a severe error has occured.

启动android模拟器时.有时会报The connection to adb is down, an ...

solr error logs org.apache.solr.common.SolrException: ERROR: [doc=17] unknown field alias

在solr中 添加新的索引词语时,报如标题所示错误,指定是插入的字段没有在solr索引字段里 可以修改 ...

Rails 风格指导

感谢译者。 本页用于介绍 Ruby 社区首推的Rails代码编写风格,翻译来自:https://git ...

Solr安装异常:SolrException: Error loading class 'solr.VelocityResponseWriter'

解决方法安装Solr过程出现错误,报异常 org.apache.solr.common.SolrExc ...

最新问答

更多

python的访问器方法有哪些

使用方法: class A(object): def foo(self,x): #类实例方法 print "executing foo(%s,%s)"%(self,x) @classmethod def class_foo(cls,x): #类方法 print "executing class_foo(%s,%s)"%(cls,x) @staticmethod def static_foo(x): #静态方法 print "executing static_foo(%s)"%x调用方法: a =

使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)

我认为你必须将两个TableGetway传递给UserTable构造。 你必须改变Module.php看看: public function getServiceConfig() { return array( 'factories' => array( 'User\Model\UserTable' => function($sm) { $userTableGateway = $sm->get('UserTable

透明度错误IE11(Transparency bug IE11)

这是一个渲染错误,由使用透明度触发,使用bootstrap用于在聚焦控件周围放置蓝色光环的box-shadow属性。 可以通过添加以下类覆盖来解决它。 .form-control:hover { -webkit-box-shadow: 0px 0px 5px 0px rgba(0,0,255,1); -moz-box-shadow: 0px 0px 5px 0px rgba(0,0,255,1); box-shadow: 0px 0px 4px 0px rgba(0,0,255,1)

linux的基本操作命令。。。

ls 显示目录 mkdir 建立目录 cd 进入目录

响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)

将z-index设置为.main-nav这将解决您的重叠问题 .main-nav { position:relative; z-index:9; } set z-index to .main-nav This will fix your overlaping issue .main-nav { position:relative; z-index:9; }

在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)

这是因为模式规范"a"打开一个文件以便追加,文件指针在末尾。 如果您尝试从此处读取,则由于文件指针位于EOF,因此没有数据。 您应该打开"r+"进行阅读和写作。 如果在写入之前读取整个文件,则在写入更多数据时,文件指针将正确定位以追加。 如果这还不够,请探索ftell()和fseek()函数。 That is because the mode spec "a" opens a file for appending, with the file pointer at the end. If you

NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)

支持空中接口的数据速率是一回事。 在消除协议开销,等待eeprom写入以及所有需要时间的其他内容之后,您看到的数据速率是完全不同的故事。 长话短说,从标签读取或进行对等传输时的实际数据速率峰值约为2.5千字节/秒。 取决于具体的标签或对等技术,它可能比这慢很多。 The supported data-rates of the air-interface are one thing. The data-rate that you see after removing protocol overhe

元素上的盒子阴影行为(box-shadow behaviour on elements)

它看起来像只在Windows上的Chrome的错误。 我在Google Canary (Chrome 63)中也进行了测试,问题依然存在,所以有可能它不会很快修复。 这个问题是由overflow: auto引起的overflow: auto ,在你的情况下,它可以很容易地通过删除或设置为可见(默认)来解决。 但是 ,将鼠标悬停在右侧(顶部和底部)时,会出现滚动条。 一个解决方案可以设置overflow: hidden的身体,所以预期的结果是所需的。 我想指出,这不是一个很好的解决方案,但我建议暂

Laravel检查是否存在记录(Laravel Checking If a Record Exists)

这取决于您是否要以后与用户合作,或仅检查是否存在。 如果要使用用户对象(如果存在): $user = User::where('email', '=', Input::get('email'))->first(); if ($user === null) { // user doesn't exist } 如果你只想检查 if (User::where('email', '=', Input::get('email'))->count() > 0) { // user found

设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)

$scope.getData= function () { var reader = new FileReader(); reader.onload = $('input[type=file]')[0].files; var img = new Image(); img.src =(reader.onload[0].result); img.onload = function() { if(this.width > 640