3

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?

1 Answer 1

11

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 to accept_nested_attributes_for your Addendum model.

class Contract < ActiveRecord::Base
  has_many :addendum
  accepts_nested_attributes_for :addendums
end

Next, open up your contract controller, and do two things. One, build an addendum when making a new contract. Two, allow the nested attributes of addendums (assuming you're using rails 4) in your contract_params method.

class ContractController < ApplicationController
  def new
    @contract = Contract.new
    @addendum = @contract.addendums.build
  end

  protected
    def contract_params
      params.require(:contact).permit(:field1, :field2, addendums_attributes: [:id, :value, :other_field])
    end
end

Last, add the forms_for helper in your contracts form.

<%= form_for @contract do |f| %>

  <!-- contract fields -->

  Addendums:
  <ul>
    <%= f.fields_for :addendums do |addendums_form| %>
      <li>
        <%= addendums_form.label :value %>
        <%= addendums_form.text_field :value %>

        <!-- Any other addendum attributes -->

      </li>
    <% end %>
  </ul>
<% end %>

With that, you should be all set! Happy coding!

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the reply, it's working. But, on the Contract's Controller, how can I access the values of the params concerning the Addendum Attributes? Shouldn't it be params[:addendum][:value] ?
With nested params, simply use @contract.assign_params(contract_params) in either your create or update controller methods to assign the contract_params. Then you can simple call @contract.addendum.first.value to get your first addendum's value. Is that what you're looking for?
Whoops, misprint. It should be @contract.addendums.first.value. addendums should be plural. Sorry about that.
So, the Addendum is automatically created? I thought I would have to created later. In fact, on the Contract's Controller create I have if contract.save createAddendum and createAddendum is a function I've created on the controller which will get the values of the params (concerning the addendum_form) and other fields that are calculated based on other fields...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.