在fields_for:集合表单中访问nested_resource的属性

时间:2022-03-25 15:56:57

I have an Assignment model, which has_many Grade objects. I'm trying to create a form wherein the user can edit many Grade objects at once, via the Assignment they belongs_to. (Grade has a :grade attribute as well)

我有一个Assignment模型,它有很多Grade对象。我正在尝试创建一个表单,其中用户可以通过他们所属的分配一次编辑许多成绩对象。 (成绩还有:成绩属性)

= form_for @assignment do |f|
  = f.fields_for :grades do |g|
    = g.text_field :grade, class: 'input'
  = f.submit 'Submit', class: 'button is-primary'

However, I would like to access the attributes of each Grade object as it's being iterated over. For example, I would like to know the name of the Student that the Grade belongs_to, so that I can label each input with it. How would I do that?

但是,我想访问每个Grade对象的属性,因为它正在迭代。例如,我想知道Grade belongs_to的学生姓名,以便我可以用它标记每个输入。我该怎么办?

1 个解决方案

#1


1  

Assuming Student is a model and Grade has belongs_to with Student, then you can do

假设学生是一名模特而且成绩与学生有属于,那么你可以做到

<%= g.object.student.name %>

#1


1  

Assuming Student is a model and Grade has belongs_to with Student, then you can do

假设学生是一名模特而且成绩与学生有属于,那么你可以做到

<%= g.object.student.name %>