嵌套表单 - fields_for() 和符号

发布于 2024-10-31 09:34:32 字数 1345 浏览 1 评论 0原文

在过去的几个小时里,我一直在努力解决这个问题:

我有一个嵌套表单(如下所示)并且它正在工作,但对于我来说,我无法弄清楚如何访问 _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2024-11-07 09:34:32

不确定我明白你想要什么,但是......

如果你执行以下操作,你可以访问表单生成器中模型的所有变量:

f.object.your_variable

示例:

f.object.id

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:

f.object.your_variable

example:

f.object.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文