2012-05-30 5 views
0

사용자가 인보이스에 대한 지불을 입력 할 수있는 양식이 있습니다. 그래서 ...레일즈 3. 유효성 검사 오류시 렌더링

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 

그래서 render action: "new" 어딘가에서 @invoice를 액션에 추가해야한다는 것을 알고 있습니다. 어떻게해야합니까?

당신이 format.html { render action: "new" }을하기 전에

답변

2

간단히 @invoice = @payment.invoice를 추가하고 모든 해당 행위의 보기를 렌더링 관리되는 것과 다른 행동을 렌더링 호출

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 
     @invoice = @payment.invoice 
     format.html { render action: "new" } 
    end 
    end 
end 

작동합니다. 호출되는 작업의 메서드를 호출하지 않습니다. 즉

다음 format.html { render action: "new" } 단순히 만들 행동에 지정된 변수 new.html.erb로드는 def new 심지어 감동되지 않습니다. 따라서 @invoice 매개 변수는 작성 작업에서 정의하지 않았기 때문에 존재하지 않습니다.

관련 문제