Ruby on rails 无法访问集合对象';erb文件中的s值-有很多关系-RubyonRails

Ruby on rails 无法访问集合对象';erb文件中的s值-有很多关系-RubyonRails,ruby-on-rails,ruby,erb,has-many,Ruby On Rails,Ruby,Erb,Has Many,在我的应用程序中,有两个类,它们具有“和许多””关系。 通过这种设计,我可以从我的控制器(*u controler.rb)获取对象,并可以成功地传递到我的视图(*.html.erb) 但在视图部分(*.html.erb)上,我无法访问对象的集合类 我得到了以下例外: ActionView::TemplateError (You have a nil object when you didn't expect it! You might have expected an instance of A

在我的应用程序中,有两个类,它们具有“和许多””关系。 通过这种设计,我可以从我的控制器(*u controler.rb)获取对象,并可以成功地传递到我的视图(*.html.erb)

但在视图部分(*.html.erb)上,我无法访问对象的集合类

我得到了以下例外:

ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.inject) on line #31 of app/views/student/_populate.html.erb:
模型类:

class Student < ActiveRecord::Base

    has_many :Subject, :class_name=>"Subject"

    attr_accessible :first_name, :middle_name, :last_name, 

    self.table_name="students"
    set_primary_key :id

    blahblah
end

class Subject < ActiveRecord::Base

    belongs_to :Student, :foreign_key=>'student_id'

    attr_accessible :student_id, :name

    self.table_name="subjects"

    blahblah
end
class-Student“主题”
属性可访问:名字,:中间名,:姓氏,
self.table_name=“学生”
设置主密钥:id
布拉布拉赫
结束
类主题'Student\u id'
属性可访问:学生id,:名称
self.table_name=“主题”
布拉布拉赫
结束
控制器:

class StudentController < ApplicationController

    require "student.rb"

    def populate
        @pos=0

        filter_query = ''
        if !params[:firstName].blank?
            filter_query +=" first_name='" + params[:firstName].to_s + "' and"
        end

        if filter_query !=''
            filter_query= filter_query[0..filter_query.length-4]
            @stuData= Student.find(:all, :conditions=>filter_query)
        end

    end
class StudentControllerfilter\u query)
结束
结束
视图:

populate.html.erb

<div class="header">
    <div class="heading" style="float:left;width:900px;">    
        <% form_tag(:controller=>"student", :action=>"populate") do %>
            <table>
                <tr>
                    <td width="10"> <label> First Name:</label> </td>
                    <td width="40"> <%= text_field_tag('firstName') %> </td>
                </tr>
            </table>

            <input name="submitFormName" class="form_submit" type="submit" value="Search" />

        <% end %>

        <div height="10">&nbsp;</div>

    </div> 
</div>

<div class="students" style="float:left;width:1330px;">

    <% if !@stuData.blank? %>
        size = <%= @stuData.size %>
        <% if !@stuData.nil? %>
            <%= render :partial=>'populate' , :locals=>{:stuData => @stuData} %>
        <% end %>
    <% else %>
        <div>Not found...</div>
    <% end %>
</div>
<div style="overflow-y:auto;overflow-x:scroll;">              
    <table >
      <thead>
        <th>Id</th>
        <th>First Name</th>
        <th>Middle Name </th>
        <th>Last Name</th>
        <th>Subject Name</th>
      </thead>    
      <% stuData.each do |item| %>
      <tr>
        <td id="studentId"> <%= item.id %> </td>
        <td id="firstNameId"> <%= item.promotion_code %> </td>
        <td id="middleNameId"> &nbsp; </td>
        <td id="lastNameId"> &nbsp; </td>
        <td id="cityId">
          <% @var1 = item.Subject %>
          <% if @var1.nil? %>
            <% @var1.each do |pc| %>
              <%= pc.name %>
            <% end %>
          <% end %>
        </td>
      </tr>
    <% end %>
  </table>
</div>

“学生”,操作=>“填充”)do%>
名字:
尺寸=
'填充',:locals=>{:stuData=>@stuData}%>
找不到。。。
_populate.html.erb

<div class="header">
    <div class="heading" style="float:left;width:900px;">    
        <% form_tag(:controller=>"student", :action=>"populate") do %>
            <table>
                <tr>
                    <td width="10"> <label> First Name:</label> </td>
                    <td width="40"> <%= text_field_tag('firstName') %> </td>
                </tr>
            </table>

            <input name="submitFormName" class="form_submit" type="submit" value="Search" />

        <% end %>

        <div height="10">&nbsp;</div>

    </div> 
</div>

<div class="students" style="float:left;width:1330px;">

    <% if !@stuData.blank? %>
        size = <%= @stuData.size %>
        <% if !@stuData.nil? %>
            <%= render :partial=>'populate' , :locals=>{:stuData => @stuData} %>
        <% end %>
    <% else %>
        <div>Not found...</div>
    <% end %>
</div>
<div style="overflow-y:auto;overflow-x:scroll;">              
    <table >
      <thead>
        <th>Id</th>
        <th>First Name</th>
        <th>Middle Name </th>
        <th>Last Name</th>
        <th>Subject Name</th>
      </thead>    
      <% stuData.each do |item| %>
      <tr>
        <td id="studentId"> <%= item.id %> </td>
        <td id="firstNameId"> <%= item.promotion_code %> </td>
        <td id="middleNameId"> &nbsp; </td>
        <td id="lastNameId"> &nbsp; </td>
        <td id="cityId">
          <% @var1 = item.Subject %>
          <% if @var1.nil? %>
            <% @var1.each do |pc| %>
              <%= pc.name %>
            <% end %>
          <% end %>
        </td>
      </tr>
    <% end %>
  </table>
</div>

身份证件
名字
中名
姓
主题名称
我在控制台上进行了交叉检查:

@student = Student.find(144)
=> #<Student id: 4, first_name: "ABC", middle_name: "DEF", last_name: "GHI">
>> 
?> @isNullCheck = @student.Subject.nil?
=> false
>> 
?> @subList = @student.Subject
=> [#<Subject id: 5, student_id: 4, name: "Maths">, #<Subject id: 6, student_id: 4, name: "English"> ]
>> 
?> @StuSub1 = @student.Subject[0]
=> #<Subject id: 5, student_id: 4, name: "Maths">
>> 
?> @StuSub2 = @student.Subject[1]
=> #<Subject id: 6, student_id: 4, name: "English">
==
?> @StuSub3 = @student.Subject[2]
=> nil
>> 
>> @StuSubValue1 = @student.Subject[0].value
=> Maths
>> 
>> @StuSubValue2 = @student.Subject[0].value
=> English
>> 
@student=student.find(144)
=> #
>> 
?>@isNullCheck=@student.Subject.nil?
=>错误
>> 
?>@subList=@student.Subject
=> [#, # ]
>> 
?>@StuSub1=@student.Subject[0]
=> #
>> 
?>@StuSub2=@student.Subject[1]
=> #
==
?>@StuSub3=@student.Subject[2]
=>零
>> 
>>@StuSubValue1=@student.Subject[0]。值
=>数学
>> 
>>@StuSubValue2=@student.Subject[0]。值
=>英语
>> 
问题: 当我从页面(populate.html.erb)搜索任何学生详细信息时,我的控制器将获取数据并将对象(@stuData)传递给模板(_populate.html.erb)

模板(_populate.html.erb)可以打印学生的数据。 但不能打印学生的科目名称。(因为学生有很多科目)

我在谷歌上搜索了很多东西

我认为模型的设计和控制器部分没有什么问题。。。。 但我认为问题可能出在

  • 页面呈现或
  • 传递给模板或模板的本地参数
  • 集合参数正在传递到模板
但我不确定

有人能帮我吗


提前感谢,

您的关系设置不正确。变量名不能以大写字母开头。也许会有帮助

设置它们时不使用大写字母,而是使用复数-如下所示:

有很多科目
并且
属于:学生


并确保通过您的视图传播更改。。。。。。。。。你太棒了。它解决了我的问题……我不知道ruby stuff对单数复数和大小写敏感度要求如此严格……在重启服务器之后。。。同样的问题开始了。。。所以我认为这不是问题所在……你是说当你进行这些更改时,问题似乎已经解决了,但在服务器重新启动后又重新出现了?是的……没错。还有一件奇怪的事情发生了:每当我启动我的服务器,在第一个请求的时候,它就会检索集合,并且工作得非常好。。。但是从第二个请求…我得到了空指针异常…所以我怀疑。。。我的响应处理/渲染机制有问题?在响应/页面处理/呈现时,我可能会错过一些基本/重要的方面…在我的情况下。。。搜索页面和搜索结果页面相同。这会造成这个问题吗??