Adding records to a nested model through an association

37 vues
Accéder directement au premier message non lu

John Sanderbeck

non lue,
14 août 2018, 17:49:0214/08/2018
à Ruby on Rails: Talk
Not sure if the subject is correct for what I am trying to do...

I am working on a School application and I added an Attendance section

There is an attendance record that is associated to a student record

The student record has an associated model called notes...

What I would like to be able to do is add notes to the student through the attendance record...

My Attendance record belongs_to :student
My Student record has_many :attendances
My Student record has_many :notes

I can access Attendance.student.notes just fine, but when I try to add notes through a nested form it doesn't like that...

Also just tried adding has_many :notes, through: :student and now I can access Attendance.notes, however when I add

      <%= f.fields_for :notes %>
      <%= f.link_to_add "Add Note", :notes, class: 'btn btn-xs btn-primary btn-padded' %>

I get an error no block given(yield)

Any suggestions on how to code the nested form?   Is this a routes issue?

John

Walter Lee Davis

non lue,
14 août 2018, 20:03:5714/08/2018
à rubyonra...@googlegroups.com

> On Aug 14, 2018, at 5:49 PM, John Sanderbeck <band...@gmail.com> wrote:
>
> Not sure if the subject is correct for what I am trying to do...
>
> I am working on a School application and I added an Attendance section
>
> There is an attendance record that is associated to a student record
>
> The student record has an associated model called notes...
>
> What I would like to be able to do is add notes to the student through the attendance record...
>
> My Attendance record belongs_to :student
> My Student record has_many :attendances
> My Student record has_many :notes
>
> I can access Attendance.student.notes just fine, but when I try to add notes through a nested form it doesn't like that...
>
> Also just tried adding has_many :notes, through: :student and now I can access Attendance.notes, however when I add
>
> <%= f.fields_for :notes %>

You need to add a 'do' at the end of the previous line. Without that, and...

> <%= f.link_to_add "Add Note", :notes, class: 'btn btn-xs btn-primary btn-padded' %>

an end here to close the block, you're not giving a black to the fields_for macro. That's what the error means.

Walter

>
> I get an error no block given(yield)
>
> Any suggestions on how to code the nested form? Is this a routes issue?
>
> John
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/038295ce-0ea0-433a-8df0-7e29e681d934%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

John Sanderbeck

non lue,
15 août 2018, 06:26:3115/08/2018
à Ruby on Rails: Talk
That's the way all my other nests are defined and they work fine...  I think the problem is I am adding a nest from a different model than I am actually working on...

John Sanderbeck

non lue,
15 août 2018, 06:42:3815/08/2018
à Ruby on Rails: Talk
The way the nests are working now is it should render the file _note_fields.html.erb but it acts like it is not finding that file which tells me the fields_for is looking for a different name due to the way it is nested

John Sanderbeck

non lue,
16 août 2018, 16:59:5916/08/2018
à Ruby on Rails: Talk
I figured it out...   Didn't have a accepts_nested_attributes_for :notes in my attendance model...  Duh !!!

However when I try to write I get an error 

Cannot modify association 'Attendance#notes' because the source reflection class 'Note' is associated to 'Student' via :has_many.

John Sanderbeck

non lue,
17 août 2018, 09:47:0517/08/2018
à Ruby on Rails: Talk
Is this the proper relationship for this to work?   It looks right to me...

What I want to do is add notes to a student through the Attendance Edit form

class Attendance < ActiveRecord::Base
        belongs_to :student
        accepts_nested_attributes_for :student
        has_many :notes, through: :student
        accepts_nested_attributes_for :notes
end

class Student < ActiveRecord::Base
        # Also used through Attendance Edit
        has_many :attendances, dependent: :destroy
        has_many :notes, dependent: :destroy
        accepts_nested_attributes_for :notes, allow_destroy: true
end

class Note < ActiveRecord::Base
        belongs_to :student
end
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message