簡體   English   中英

Rails 3:視圖中的Ruby變量在NilClass中

[英]Rails 3: Ruby variables in view are in NilClass

請原諒新手問題,但我很困惑。 我是一位轉換Rails的老師
1個應用程序連接到Rails3。我的“驗​​證”控制器正在從數據庫中獲取正確的值,
但是當視圖試圖訪問變量時,我被告知它們在“ 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?

您正確使用了變量,但是錯誤提示Assignment.get_student返回nil,而不是學生。 在Assigment模型中檢查此方法。

另外,不確定這是否是粘貼錯誤,但您的控制器應如下所示:

class VerifyController < ApplicationController

  def list
    @student = Assignment.get_student
    @assignments = Assignment.get_assignments
    logger.info("VERIFY: @student is #{@student}")
    logger.info("VERIFY: @assignments is #{@assignments}")
  end

end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM