Question

I have created a nested form. I have a model called contract and another called contract_details. In my contract controller I have the following code:

def new
    @contract = Contract.new
    @contract.customer_id = params[:customer_id]
    7.times { 
      @contract.contract_details.build
      @contract.contract_details.del_day = "Sun"
    }
end

del_day is a column in my contract_details model but the following line of code is erring out:

@contract.contract_details.del_day = "Sun"

What am I doing wrong? How do I access a column in the nested model from the top controller. Meaning, how do I set the value of a column in the contract_details model when building each row in the contracts controller?

Was it helpful?

Solution

When I re-read, you would only be doing the same thing 7 times if it was an array, so suggest you try the following:

def new
    @contract = Contract.new
    @contract.customer_id = params[:customer_id]
    7.times { 
      @new_contract_details = @contract.contract_details.build
      @new_contract_details.del_day = "Sun"
    }
end

The issue would be that at present when you call the following line, there is no indication which of the 'contract_details' objects you are trying to update.

@contract.contract_details.del_day = "Sun"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top