ユーザーが請求書の支払いを入力できるフォームがあります。そう...
invoice has_many :payments
payment belongs_to :invoice
問題は、検証エラーが発生した場合、ユーザーが必要な支払い日を入力しなかったとしましょう。このエラーが発生します...
undefined method invoice_number for nil:NilClass
Extracted source (around line #10):
7:
8: <div class="content-box-content">
9: <div class="tab-content default-tab" style="display: block; ">
10: <h4>Invoice #<%= @invoice.invoice_number %> for <%= @invoice.company.name %></h4>
11: <p>Invoice Balance $<%= sprintf("%.2f", @invoice.balance) %></p>
12: </div>
13: </div>
payments_controller.rb
----------------------
def new
@invoice = Invoice.find(params[:invoice_id])
@payment = @invoice.payments.build
respond_to do |format|
format.html
end
end
def create
@payment = Payment.new(params[:payment])
respond_to do |format|
if @payment.save
format.html { redirect_to payments_path, notice: 'Payment was successfully created.' }
else
format.html { render action: "new" }
end
end
end
したがって、 @invoice を create アクションのどこかに追加する必要があることはわかっていますrender action: "new"
。これどうやってするの?