繁体   English   中英

无法访问erb文件中的集合对象的值-has_many关系-Rails上的ruby

[英]Can not access collection object's value in erb file - has_many relationship - ruby on rails

在我的应用程序中,有两个类具有“ has_many ”关系。 通过这种设计,我可以从控制器(* _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 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

视图:

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>

_populate.html.erb

<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
>> 

问题:当我从页面(populate.html.erb)搜索任何学生详细信息时,我的控制器将获取数据并将对象(@stuData)传递给模板(_populate.html.erb)。

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

我用Google搜索了很多东西。

我认为模型的设计和控制器部分没有错....但是我认为问题可能出在

  • 页面渲染或
  • 传递给模板的本地参数或
  • 集合参数传递给模板...

但我不确定...

有人可以帮我吗???

提前致谢,

您的人际关系设置不正确。 变量名不能以大写字母开头。 本文可能会有所帮助。

在不使用大写字母的情况下进行设置,而是使用复数形式-像这样:

has_many :subjectsbelongs_to :student

并确保您通过视图传播更改

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM