Paul N
Paul N

Reputation: 623

Accessing attributes of a nested_resource in a fields_for :collection form

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)

= 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?

Upvotes: 1

Views: 143

Answers (1)

Pavan
Pavan

Reputation: 33552

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

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

Upvotes: 2

Related Questions