我一直在为此而努力的最后几个小时:嵌套表格 - fields_for()和符号
我有一个嵌套的表格(如下图所示),它的工作,但对我的生活,我不能请参阅如何访问_form_outcome_ratings.html.erb中的:student_id。它会在创建的隐藏字段中显示相应的student_id,但我不知道如何访问该号码以在评分字段旁边显示学生的姓名。当我尝试引用:student_id或:student_id.to_s时,它会返回“student_id”而不是数字。我想我错过了或误解了一些非常基本的东西,但我无法弄清楚什么!
感谢您抽出宝贵时间看一下,并告诉我是否需要澄清或添加任何内容。
/app/views/learning_outcomes/_form_rate.html.erb
<% form_for(@learning_outcome) do |f| %>
<% f.fields_for :outcome_ratings do |g| %>
<%= render :partial => 'form_outcome_ratings', :locals => {:f => g} %>
<% end %>
<%= f.submit %>
<% end %>
/app/views/learning_outcomes/_form_outcome_ratings.html.erb
<%= f.hidden_field :student_id %>
<%= f.label :rating %><%= f.text_field :rating %>
/app/controllers/learning_outcomes_controller.rb
def rate
@learning_outcome = LearningOutcome.find(params[:id], :include => {:section => {:students => {:outcome_ratings => [:learning_outcome, :student]}}})
@learning_outcome.section.students.each do |student|
@learning_outcome.outcome_ratings.build(:student_id => student.id) if student.outcome_ratings.where(:learning_outcome_id => @learning_outcome.id).blank?
end
end
谢谢!这(基本上)正是我所期待的。嵌套部分中的f.object.student_id返回我试图获取的值。 – niiru 2011-04-08 19:28:44