(Sorry if this has been addressed before, can't find it.)
Let's say I've got three tables. I'll keep it simple (P = Post
, C = Comment
and U = User
, but not what I'm actually developing): P ||-> C <-|| U
, where P
can have many C
s, and U
can have many C
s. I've got my resource routes setup as Ps/[:p_id]/Us/[:u_id]/cs/[:c_id]
. I need to create a /new
C
. From my understanding, typically if I was only building C
from only P
or U
, I would just generate it from an P.c.build
/U.c.build
. But since I need both, and neither A
nor C
are directly hierarchical to each other, I'm trying to understand how to do this. I need three things:
*_path
helper generated somehow with a new_p_u_c(@P, @U)
.build
alternative for triangulating both P
and U
with C
.form_with
:
model:
with @P
and @U
url:
*_path
create helper (p_u_cs(@P, @U)
).P
and U
, is will that be automatically generated within the forms_with
's <form>
?If anyone finds this, hope it helps.
new_p_u_c_path(p_id: @P.id, u_id: @U.id)
. I wasn't aware that the paths helper was capable of dynamically accepting multiple arguments. Precautions:
friendly_id
), you'll need to pass in the @P.id
specifically instead of @P
, or else the path helper won't be able to find it.@P.c.build
for this. It worked, but I'm not sure if it is even necessary at this point, since the only thing I needed was the @P.id
, so see below...form_with(model: @C, ...)
/new
route already contained the @P.id and @U.id, the hidden_field
automatically accepted p_id
and u_id
since I had my models setup with appropriate chaining logic.p_u_cs_path
worked without passing any additional model arguments (again, see 3.1. above).