Rails 3:视图中的 Ruby 变量位于 NilClass 中
请原谅新手的问题,但我很困惑。我是一名转换 Rails 的老师
1 个应用程序到 Rails 3。我的“验证”控制器正在从我的数据库获取正确的值,
但是当视图尝试访问变量时,我被告知它们位于“nilClass”中。
这是我的控制器:
文件 ROOT/app/controllers/verify_controller.rb:
class VerifyController < ApplicationController
@student = Assignment.get_student
@assignments = Assignment.get_assignments
logger.info("VERIFY: @student is #{@student}")
logger.info("VERIFY: @assignments is #{@assignments}")
end
这是我的视图:
文件 ROOT/app/views/verify/list.html.erb:
<?xml version="1.0" encoding="UTF-8" ?>
<!--
This is our listing view.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Student assignments</title>
</head>
<body>
<div id="assignment-list">
<!-- @student is a Ruby variable. The @ means it is a "member"
which is accessible beyond the code that calculates it. (The
class is the VerifyController class, which we will be seeing
shortly.) See below for the angle brackets!
-->
<h2>Assignments for <%= @student %></h2>
<table cellspacing="5" cellpadding="5" border="2">
<tr>
<th>Student</th>
<th>Description</th>
<th>Date received</th>
</tr>
<!-- HTML alone doesn't know how many rows are in the table, so
Ruby has to look at the variables and provide a loop to do
the rows .
-->
<!-- < % and % > delimit Ruby code in HTML so long as the Ruby
doesn't produce a value (there is no blank space between
the angle brackets and the % signs; I had to put one in
to prevent Ruby from trying to interpret this comment!)
If the expression does produce a value, < % = and % > are the
delimiters.
-->
<% logger.info("LIST: The class of @student is #{@student.class}") %>
<% logger.info("LIST: The class of @assignments is #{@assignments.class}") %>
<% logger.info("LIST: @student is #{@assignments}") %>
<% logger.info("LIST: @assignments is #{@assignments}") %>
<% for assign in @assignments %>
<tr>
<td><%= assign.student %></td>
<td><%= assign.description %></td>
<td><%= assign.whendone %></td>
</tr>
<% end # end the "for" %>
</table>
</body>
</html>
Any suggestions?
Please pardon the newbie question, but I'm stumped. I'm a teacher converting a Rails
1 app to Rails 3. My "verify" controller is getting correct values from my database,
but when the view attempts to access the variables I'm told they are in "nilClass".
Here's my controller:
file ROOT/app/controllers/verify_controller.rb:
class VerifyController < ApplicationController
@student = Assignment.get_student
@assignments = Assignment.get_assignments
logger.info("VERIFY: @student is #{@student}")
logger.info("VERIFY: @assignments is #{@assignments}")
end
Here is my view:
file ROOT/app/views/verify/list.html.erb:
<?xml version="1.0" encoding="UTF-8" ?>
<!--
This is our listing view.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Student assignments</title>
</head>
<body>
<div id="assignment-list">
<!-- @student is a Ruby variable. The @ means it is a "member"
which is accessible beyond the code that calculates it. (The
class is the VerifyController class, which we will be seeing
shortly.) See below for the angle brackets!
-->
<h2>Assignments for <%= @student %></h2>
<table cellspacing="5" cellpadding="5" border="2">
<tr>
<th>Student</th>
<th>Description</th>
<th>Date received</th>
</tr>
<!-- HTML alone doesn't know how many rows are in the table, so
Ruby has to look at the variables and provide a loop to do
the rows .
-->
<!-- < % and % > delimit Ruby code in HTML so long as the Ruby
doesn't produce a value (there is no blank space between
the angle brackets and the % signs; I had to put one in
to prevent Ruby from trying to interpret this comment!)
If the expression does produce a value, < % = and % > are the
delimiters.
-->
<% logger.info("LIST: The class of @student is #{@student.class}") %>
<% logger.info("LIST: The class of @assignments is #{@assignments.class}") %>
<% logger.info("LIST: @student is #{@assignments}") %>
<% logger.info("LIST: @assignments is #{@assignments}") %>
<% for assign in @assignments %>
<tr>
<td><%= assign.student %></td>
<td><%= assign.description %></td>
<td><%= assign.whendone %></td>
</tr>
<% end # end the "for" %>
</table>
</body>
</html>
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正确使用了变量,但错误表明
Assignment.get_student
返回 nil 而不是学生。在分配模型中检查此方法。另外,不确定这是否是粘贴错误,但您的控制器应该如下所示:
You are using the variables correctly, but the error would suggest that
Assignment.get_student
is returning nil instead of a student. Check this method in the Assigment model.Also, not sure if it was a paster error, but your controller should look like this: