Question

I am trying to build a nested form in rails 4. I've got the view up and running however when I submit the form the values do not save to my database. I read through the following answer and tried to replicate it in my code but I am still having the same issue:

Rails 4 nested attributes not saving

Here are what I think the relevant pieces of code:

View:

<div class="field">
<%= f.label :imagefile %><br>
<%= f.text_area :imagefile %>
</div>

<%= f.fields_for :amount_due do |ff| %>
 <div class="field">
  <%= ff.label :amount_due %><br>
  <%= ff.text_field :amount_due %>
 </div>

 <div class="field">
  <%= ff.label :invoice_id %><br>
  <%= ff.text_field :invoice_id %>
 </div>
<% end %>

invoices_controller:

def new
 @invoice = Invoice.new
 @invoice.amount_dues.build 
end

def invoice_params
  params.require(:invoice).permit(:imagefile, :user_id,
    :amount_dues_attributes => [:id, :amount_due, :invoice_id])
end

amount_due Model:

class AmountDue < ActiveRecord::Base
 belongs_to :invoice
 belongs_to :user
end

invoice Model:

class Invoice < ActiveRecord::Base
 belongs_to :user
 validates :user_id, presence: true
 has_many :amount_dues
 accepts_nested_attributes_for :amount_dues
end
Was it helpful?

Solution

Figured it out. I did not make plural :amount_due in my view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top