我有一个这样的控制器:
def show
@professor = Professor.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name"
end
end
end
像这样的简单视图:
<p>Professor: <%= @professor.first_name %></p>
<p>Email: <%= @professor.email if @professor.email %></p>
我也有一个布局'application.html.erb';
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Myapp" %></title>
<meta name="description" content="">
<meta name="author" content="">
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= yield(:head) %>
</head>
<body>
<header class="navbar navbar-fixed-top">
<nav class="navbar-inner">
<div class="container">
<%= render 'layouts/navigation' %>
</div>
</nav>
</header>
<div id="main" role="main">
<div class="container">
<div class="content">
<div class="row">
<div class="span12">
<%= render 'layouts/messages' %>
<%= yield %>
</div>
</div>
<footer>
</footer>
</div>
</div> <!--! end of .container -->
</div>
当我执行以下命令时:
bundle exec wkhtmltopdf 'http://local.myapp.com:3000/professors/2' - > test.pdf
我得到了一个包含所有样式和布局的pdf。
但是,当我去时,http://local.myapp.com:3000/professors/2.pdf
我收到一条错误消息:
Missing template professors/show with {:locale=>[:en], :formats=>[:pdf], :handlers=>[:erb, :builder, :coffee]}
所以,然后我将我的控制器#show 操作更改为如下所示:
def show
@professor = Professor.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name",
:template => 'professors/show.html.erb'
end
end
end
这有助于我渲染视图,但不幸的是它不会渲染样式和布局。我的问题是:
- 为什么我需要指定模板?
- 为什么布局没有在 PDF 中呈现?