我正在尝试在rails 4中构建嵌套表单。我已经启动并运行了视图但是当我提交表单时,值不会保存到我的数据库中。我仔细阅读了以下答案并尝试在我的代码中复制它,但我仍然遇到同样的问题:
Rails 4 nested attributes not saving
以下是我认为的相关代码:
查看:
<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模型:
class AmountDue < ActiveRecord::Base
belongs_to :invoice
belongs_to :user
end
发票型号:
class Invoice < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
has_many :amount_dues
accepts_nested_attributes_for :amount_dues
end
答案 0 :(得分:1)
想出来。我没有复数:在我看来是amount_due。