nil的未定义方法:NilClass

时间:2013-01-19 19:01:10

标签: ruby-on-rails-3 undefined has-many belongs-to

上传控制器

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 #create中的NoMethodError],[nil的未定义方法`uploads':NilClass]

有谁知道我在做什么?

1 个答案:

答案 0 :(得分:0)

在上传模型中声明belongs_to :order 意味着将在控制器中自动定义@order。也许您想要的是控制器中的before_filter,它将设置@order