嵌套表单 - 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
<%= f.hidden_field :student_id %>
<%= f.label :rating %><%= f.text_field :rating %>
/views/learning_outcomes/_form_outcome_ ratings.html.erb /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
I've been struggling with this for the last several hours:
I have a nested form (shown below) and it's working, but for the life of me, I cannot figure how to access the :student_id in the _form_outcome_ratings.html.erb. It displays the appropriate student_id in the hidden field that's created, but I have no idea how to access that number to display the student's name next to the rating field. When I try to reference :student_id or :student_id.to_s it returns "student_id" instead of the number. I think I'm missing or misunderstanding something very basic, but I can't figure out what!
Thank you for taking the time to look at this, and let me know if there's anything I need to clarify or add.
/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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定我明白你想要什么,但是......
如果你执行以下操作,你可以访问表单生成器中模型的所有变量:
示例:
Not sure I understod what you want, but...
You can access all variables of a model within a form builder if you do as follows:
example: