表格中的Rails 4嵌套属性未保存 [英] Rails 4 nested attribute in a form not saving
本文介绍了表格中的Rails 4嵌套属性未保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Rails 4中构建嵌套表单.我已经启动视图并运行了,但是当我提交表单时,值不会保存到我的数据库中.我通读了以下答案,并尝试将其复制到我的代码中,但仍然遇到相同的问题:
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:
以下是我认为相关的代码段:
Here are what I think the relevant pieces of code:
查看:
<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 %>
发票控制器:
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
金额到期模型:
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
推荐答案
弄清楚了.我认为:amount_due没有复数.
Figured it out. I did not make plural :amount_due in my view.
这篇关于表格中的Rails 4嵌套属性未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文