I have two models Contract
and Addendum
. Contract has_many :addendums
and Addendum belongs_to :contract
When a new Contract is created, automatically will create a new Addendum but some aditional elements are needed to create the new Addendum. How can I add a field value
, which is an attribute from Addendum but not from Contract, on the Contract's form?
What you're looking for is a nested form, which is pretty common in RoR. For more information on nested and complex forms, there's a section of a Rails Guide for that. I'd recommend checking out all of the Rails Guides, which are incredibly helpful when learning the framework.
For your specific question, first tell your
Contract
model toaccept_nested_attributes_for
yourAddendum
model.Next, open up your contract controller, and do two things. One, build an
addendum
when making a newcontract
. Two, allow the nested attributes ofaddendums
(assuming you're using rails 4) in yourcontract_params
method.Last, add the
forms_for
helper in yourcontract
s form.With that, you should be all set! Happy coding!