我被困了两天......
我的问题是: 我无法在.erb
打印嵌套的集合对象的值让我解释一下我的情况:
用例:我有一个搜索页面,我可以从中搜索(例如:按姓名)学生的信息。
输出:学生名单。 (每个学生都有一个科目列表)
问题:ON视图部分,在.erb文件中: - 我可以打印学生的姓名,但不能打印主题的名称。
这是我文件的片段......
模型类:
class student < ActiveRecord::Base
has_many :subjects, :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"> </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"> </td>
<td id="lastNameId"> </td>
<td id="cityId">
<% @var1 = item.Subject %>
<% if @var1.nil? %>
<% @var1.each do |pc| %>
<%= pc.name %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</table>
</div>
当响应包含“集合”时,我不知道如何“呈现页面”,其中“集合的每个对象”都包含另一个“集合”
回复:学生名单 收藏:学生 每个学生对象:包含集合(主题列表)
有人请帮助.......
修改
我尝试了以下渲染集合的方法......但无法取得成功。
- <%= render :partial=>'populate' , :locals=>{:stuData =>
@stuData} %>
- <%= render :partial=>'populate' , :collection=>
@stuData.subjects %>
- <% render :partial=>'populate' , :collection=>
@stuData.subjects %>
- <%= render @stuData %>
- <%= render :partial=>"populate", :collection=>@stuData,
:as=>:item %>
第二次编辑
由于问题没有通过以下答案解决....我已经与数据库和控制台交叉检查......它看起来还不错。
@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
>>
编辑第三次......
至少可以帮助我如何在erb中呈现页面...当响应具有对象数组时,其中每个对象本身都包含另一个集合。 (这里是一群学生。每个学生都有各种科目。)
任何???
答案 0 :(得分:1)
你应该可以做这样的事情
stuData.each do |stud|
stud.name
bla bla bla...
stud.subjects.each do |sub|
sub.name
end
end
而不是这个,你没有使用你在模型中定义的关联
var1 = item.Subject
var1.each ....
编辑:正如你所说的问题是你传递给你的部分的集合。作为@Rich Peck 建议你,你应该传递像:
<%unless @stuData.nil? %>
<%= render :partial=>'populate', :stuData => @stuData %>
<%end%>
答案 1 :(得分:1)
<强>模型强>
- 需要CamelCase -
Class Student < ActiveRecord::Base
- 需要简化:
醇>
class Student < ActiveRecord::Base
has_many :subjects, :class_name=>"subject"
attr_accessible :first_name, :middle_name, :last_name,
end
class Subject < ActiveRecord::Base
belongs_to :student
attr_accessible :student_id, :name
end
在数据表中,您应该具有以下架构:
students
id | first_name | middle_name | last_name | created_at | updated_at
subjects
id | student_id | name | etc | created_at | updated_at
<强>视图强>
这将允许您显示:
@stuData = Student.all
<%= render partial: "populate", collection: @stuData, as: :stuData %>
#_populate.html.erb
<% stuData.each do |student| %>
<%= "#{student.first_name} #{student.last_name}" %>
<% student.subjects.each do |subject| %>
<%= subject.name %>
<% end %>
<% end %>
<强>重构强>
您的代码可以使用一些重构:
<% unless @stuData.blank? %>
<%= "size = #{@stuData.size}" %>
<%= render :partial=>'populate' , :locals=>{:stuData => @stuData} unless @stuData.nil? %>
<% else %>
<div>Not found...</div>
<% end %>
<强>更新强>
我认为您的问题在于您的控制器操作:
def populate
unless params[:firstName].blank?
filter_query +=" first_name='" + params[:firstName].to_s + "' and"
filter_query= filter_query[0..filter_query.length-4]
@stuData = Student.where(first_name: filter_query) unless filter_query.blank?
#only sets @stuData if filter_query is present. It's likely that filter_query is not present for you
end
end