2013-01-19 1 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 

만들고 난 UploadtwosController 번호에 오류 [NoMethodError 얻을 ], [ 정의되지 않은 메소드`uploads 'for nil : NilClass]

아무도 모르는 것을 알고 있습니까?

답변

0

그냥 업로드 모델에 belongs_to :order를 선언하는 것은 하지@order가 자동으로 컨트롤러로 정의 될 것을 의미한다. 아마도 컨트롤러에서 before_filter을 입력하면 @order이 설정됩니까?

+0

매우 빠른 응답을 주셔서 감사합니다. 어떻게 정의할까요? 새로운 레일입니다. 이것이 제 첫 번째 앱입니다. –

+0

컨트롤러의 어딘가에'before_filter {@order = Order.new (...)}'와 같은 것을 원할 것입니다. '... '대신 정확히 들어가야 할 것은'@ order'의 기본값을 원하는 데이터에 달려 있습니다. 자세한 내용은 Rails 안내서의 "before_filter"를 참조하십시오. –

+0

정말 감사합니다. –