Rails 3:我的视图没有被渲染
Posted
技术标签:
【中文标题】Rails 3:我的视图没有被渲染【英文标题】:Rails 3: My view is not being rendered 【发布时间】:2012-05-19 01:35:53 【问题描述】:我是 Rails 新手。我有我的控制器和视图设置。
当我查看我的 html 源代码时,我的正文是空的。我在我的控制器和我的视图中放置了一个调试器,它同时命中了这两个,所以我知道它正在被执行。 知道我做错了什么吗? 我也安装了引导程序。
提前谢谢你。 见下文。
class ListController < ApplicationController
def recent
@page_title = @head_title="Lists"
end
end
recent.html.erb
<ul class="tabs">
<li class="active"><a href="#">Lorem</a></li>
<li><a href="#">Ipsum</a></li>
<li><a href="#">Dolor</a></li>
<li><a href="#">Sit</a></li>
<li><a href="#">Amet</a></li>
</ul>
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>
</html>
html源代码
<!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>Myapp</title>
<meta name="description" content="">
<meta name="author" content="">
<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/custom.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/home.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/twitter/bootstrap.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/alert.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/button.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/carousel.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/collapse.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/dropdown.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/modal.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/tooltip.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/popover.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/scrollspy.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/tab.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/transition.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/typeahead.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-transition.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-alert.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-button.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-carousel.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-collapse.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-dropdown.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-modal.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-scrollspy.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-tab.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-tooltip.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-popover.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-typeahead.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="7imEFVfD5bYIV7ZcK4181eCoz0pKxyyE2G+UFI46v5U=" name="csrf-token" />
</head>
</html>
来自服务器日志
2012 年 5 月 18 日星期五 17:54:57 -0700 开始为 127.0.0.1 获取“/list/recent” ListController#recent 作为 HTML 处理 在布局/应用程序中渲染列表/recent.html.erb (0.8ms) 在 17 毫秒内完成 200 次 OK(查看次数:16.9 毫秒 | ActiveRecord:0.0 毫秒)
【问题讨论】:
【参考方案1】:嗯,您的 application.html.erb 文件中没有<body>
标签或任何视图内容? :-)
尝试在您的 </html>
之前添加此内容:
<body>
<%= yield %>
</body>
不带参数的yield
将从渲染视图中获取内容并将其吐出到您的应用程序布局中。
【讨论】:
以上是关于Rails 3:我的视图没有被渲染的主要内容,如果未能解决你的问题,请参考以下文章