在独立嵌套模型视图中访问父属性

时间:2013-05-03 09:15:59

标签: ruby-on-rails ruby ruby-on-rails-3 associations

我有嵌套资源

 resources :invoices do
  resources :payments
 end

发票模型如下:

 class Invoice < ActiveRecord::Base

belongs_to :customer, :inverse_of => :invoices
attr_accessible :due_date, :invoice_date, :reading_ids, :customer_id, :customer, :status, :amount, :balance

has_many :invoice_items, :dependent => :destroy
has_many :payments, :dependent => :destroy
 end

付款模式如下:

class Payment < ActiveRecord::Base
 attr_accessible :amount, :method, :payment_date, :reference_no, :invoice_id
 belongs_to :invoice
end

付款控制人如下:

class PaymentsController < ApplicationController
before_filter :authenticate_user!

def new
    invoice = Invoice.find(params[:invoice_id])
    @payment = invoice.payments.build
respond_to do |format|
  format.html #new.html.erb 
end 
  end
    end

我创建了一个记录新付款的视图,并希望在此视图中显示客户详细信息(特别是名称),我该如何处理?

付款视图

<%= simple_form_for [@payment.invoice, @payment], :html => { :class => 'form-horizontal' } do |f| %>
<%= render "shared/error_messages", :target => @payment %>

<h5> Invoice Details </h5>
<%= f.input :invoice_id,  disabled: true,  as: :string %>
<%= f.input :method, as: :select, :collection => [['Cash','Cash'],['Cheque','Cheque'],['In-House transfer','In-House transfer'],['Account Ledger','Account ledger']], :selected => ['Cash','Cash'] %>
  <%= f.input :reference_no, as: :string %>
  <%= f.input :payment_date, as: :string, input_html: { class: "datepicker" } %>
<% end %>

1 个答案:

答案 0 :(得分:0)

只需使用:

<%= @payment.invoice.customer.name %>

视图中的任何地方。