2013-01-19 96 views
0

上傳控制器爲無未定義的方法:NilClass

def create 
@upload = @order.uploads.build(params[:uploadtwo]) 
    respond_to do |format| 
    if @upload.save 
    format.html { redirect_to root_path, :notice => 'File was successfully uploaded.' } 
    format.json { render :json => @upload, :status => :created, :location => @upload } 
    else 
    format.html { render :action => "new" } 
    format.json { render :json => @upload.errors, :status => :unprocessable_entity }  
end 

上傳模型

belongs_to :order 

階模型

has_many :uploads 

我得到錯誤[NoMethodError在UploadtwosController#創建],[未定義的方法`上傳'爲零:NilClass]

有人知道我在做什麼?

回答

0

只需在您的上傳模型中聲明belongs_to :order即可而不是意味着@order會在控制器中自動定義。也許你想要的是一個before_filter在控制器將設置@order

+0

感謝您的快速響應。我將如何定義它?與軌道新,這是我的第一個應用程序。 –

+0

在你的控制器的某個地方,你會想要一些類似於'before_filter {@order = Order.new(...)}'的東西。究竟應該用什麼來代替'...',取決於你希望使用默認的'@ order'的數據。請參閱Rails指南中的「before_filter」以獲取更多信息。 –

+0

非常感謝,就此 –

相關問題