[英]Help with rails belongs_to
我被困-
我正在Rails中构建一个会计应用程序,并有一个模型,其中客户将具有多个发票以及多个付款(与这些发票相关)
快速浏览一下简化模型:
class Customer < ActiveRecord::Base
has_many :customer_payments
has_many :invoices
accepts_nested_attributes_for :customer_payments, :allow_destroy => true
end
class CustomerPayment < ActiveRecord::Base
has_many :customer_payment_items
belongs_to :customer
belongs_to :invoice
accepts_nested_attributes_for :customer_payment_items
end
class CustomerPaymentItem < ActiveRecord::Base
belongs_to :invoice, :inverse_of => :customer_payment_items
belongs_to :customer_payment
end
class Invoice < ActiveRecord::Base
has_many :invoice_lines, :dependent => :destroy
has_many :customer_payment_items, :inverse_of => :invoice
belongs_to :customer
accepts_nested_attributes_for :invoice_lines, :allow_destroy => true
end
我有一个嵌套的表单,我想在其中显示Customer属性,CustomerPayment属性和CustomerPaymentItem属性-都可以正常工作。
我还想显示每个CustomerPaymentItem的发票属性(每个CustomerPaymentItem都与单个发票相关),虽然我可以获取一个表格来显示CustomerPaymentItem信息,但我却无法显示它来显示需要引用的发票信息用户。 -我无法从belongs_to关联中获取数据以显示在表单上。
我不知所措-我是否应该能够遍历belongs_to关联? 仅供参考-我可以将数据发送到日志,在该日志中我知道在CustomerPayment.new调用期间填充了发票数据,似乎只是在控制器和表单之间丢失了数据。
我应该如何访问这些数据? 这是表单信息-(来自几个渲染的表单)未显示的内容在---之间。
<p>
<%= f.label :amount %><br />
<%= f.text_field :amount %>
</p>
<p>
<%= f.label :invoice_id %><br />
<%= f.text_field :invoice_id %>
</p>
<p>
<% f.fields_for :invoice do |builder| %>
--- doesn't show
Invoice Detail
<p>
<%= builder.label :number %><br />
<%= builder.text_field :number %>
</p>
<p>
<%= builder.label :sub_total %><br />
<%= builder.text_field :sub_total %>
</p>
--- doesn't show
<% end %>
</p>
我是否在fields_for中缺少显示:invoice参考数据的内容? 我的模型是否过于复杂以至于无法理解Rails?
@corroded是正确的-问题是我缺少=登录
<% f.fields_for :invoice do |builder| %>
我想每个人都需要学习的教训
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.