Hello and welcome!
In my last post, we were working on getting deeply nested forms to work without error.
Here’s a look at the error we ended on last time:
I was able to get these errors to quiet down by being a bit more explicit with my two
has_many associations on
TeacherAid by passing a parameter called
inverse_of like so:
I found some documentation here from the Rails API to give us a bit of understanding about what this options provides us.
And here’s a snippet from a this StackOverflow question as well:
If I were asked to boil these down, I would say that
inverse_of is used for explicitness, taking the guess-work off the shoulders of Rails needing to figure which class name an association is defined between.
With this error now silent, lets take a look at our form template again.
An (almost) Working Deeply Nested Form
Here we can see that all of our form fields are properly showing up.
Note the values for the teacher’s
secondary_phone form fields.
Now lets commit this form and take a look at the primary and secondary phone numbers:
Defining the Problem
So did you catch the primary and secondary phone issue from the
show template shown above? Basically I have defined two different object associations on one polymorphic table. You can see this on lines: 11 & 12 in the following illustration:
But for reasons I have yet to find insight into, both
TeacherAid‘s secondary phone numbers are reading the value entered into the primary number’s field.
Lets take a quick look at our
_form.html.erb and our
show.html.erb templates to see if I made a mistake assigning variables to POST or GET these resource representations.
This appears to be correct. I’m using the
f iterative variable on lines: 13-16 for the first set of phone numbers because they represent our parent model’s phone attributes. And on line: 21 I’m defining a nested iterative variable resource by using
f.fields_for :teacher_aids and calling
Lets peak at the
show template to see if I made an error on that page:
And things appear to be written correctly on this template as well.
@teacher.primary_phone.number and for the
TeacherAid, I’m doing the same thing with
teacher_aid as well.
Over this coming weekend, I plan to take a closer look into polymorphic tables. My goals are to find out if I cannot, in fact, define two unique objects on one polymorphic table. This doesn’t seem likely, but it will feel good to dig into some docs and blogs to see what I’m overlooking.
Have a great weekend, and stay tuned…