简体   繁体   English

Rails帮助

[英]Help with rails belongs_to

I'm stuck - 我被困-

I'm building an accounting app in rails, and have a model where a customer will have multiple invoices as well as multiple payments (related to those invoices) 我正在Rails中构建一个会计应用程序,并有一个模型,其中客户将具有多个发票以及多个付款(与这些发票相关)

Here's a quick look at a simplified model: 快速浏览一下简化模型:

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

I have a nested form where I want to show Customer attributes, CustomerPayment attributes and CustomerPaymentItem attributes - which all works fine. 我有一个嵌套的表单,我想在其中显示Customer属性,CustomerPayment属性和CustomerPaymentItem属性-都可以正常工作。

I also want to show Invoice attributes for each CustomerPaymentItem (each CustomerPaymentItem relates back to a single invoice) and while I can get a form to show the CustomerPaymentItem info, I can't get it to show Invoice information which is needed to give reference to the user. 我还想显示每个CustomerPaymentItem的发票属性(每个CustomerPaymentItem都与单个发票相关),虽然我可以获取一个表格来显示CustomerPaymentItem信息,但我却无法显示它来显示需要引用的发票信息用户。 - I'm having trouble getting data from a belongs_to association to show on the form. -我无法从belongs_to关联中获取数据以显示在表单上。

I'm at a loss - Shouldn't I be able to traverse the belongs_to association? 我不知所措-我是否应该能够遍历belongs_to关联? FYI - I can send data to the log where I know the invoice data is populated during the CustomerPayment.new call, it seems to just get lost between the controller and the form. 仅供参考-我可以将数据发送到日志,在该日志中我知道在CustomerPayment.new调用期间填充了发票数据,似乎只是在控制器和表单之间丢失了数据。

How should I access that data? 我应该如何访问这些数据? Here's the form info -- (coming from a couple of rendered forms) the stuff that doesn't show is in between the ---. 这是表单信息-(来自几个渲染的表单)未显示的内容在---之间。

<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>

Am I missing something in my fields_for to show the :invoice reference data? 我是否在fields_for中缺少显示:invoice参考数据的内容? Is my model too complex for rails to make sense of? 我的模型是否过于复杂以至于无法理解Rails?

@corroded was right - the issue was my lack of an = sign in @corroded是正确的-问题是我缺少=登录

<% f.fields_for :invoice do |builder| %>

I guess everyone needs to learn that lesson 我想每个人都需要学习的教训

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM