ruby on rails - Access parent attribute in independent nested model view -
i have nested resources
resources :invoices resources :payments end
the invoices model follows:
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
the payments model follows:
class payment < activerecord::base attr_accessible :amount, :method, :payment_date, :reference_no, :invoice_id belongs_to :invoice end
the payments controller follows:
class paymentscontroller < applicationcontroller before_filter :authenticate_user! def new invoice = invoice.find(params[:invoice_id]) @payment = invoice.payments.build respond_to |format| format.html #new.html.erb end end end
i have created view record new payments , display customer details (name in particular) in view, how go it?
payments view
<%= simple_form_for [@payment.invoice, @payment], :html => { :class => 'form-horizontal' } |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 %>
just use:
<%= @payment.invoice.customer.name %>
anywhere in view.
Comments
Post a Comment