2015-04-23 167 views
2

我有一個簡單的模型驗證問題。 輸入材料沒有SKU讓我錯誤信息:Ruby on Rails - 模型驗證

材料NoMethodError#創建未定義的方法`空?'對於 零:NilClass

enter image description here material.rb:

class Material < ActiveRecord::Base 
    validates :sku, :presence => true 
end 

materials_controler.rb(僅創建部分):

# POST /materials 
    # POST /materials.json 
    def create 
    @material = Material.new(material_params) 


    respond_to do |format| 
     if @material.save 
     format.html { redirect_to @material, notice: 'Material was successfully created.' } 
     format.json { render :show, status: :created, location: @material } 
     else 
     format.html { render :new } 
     format.json { render json: @material.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

回答

1

定義@units也在您的創建方法中

3

@units實例變量是create行動nil。在記錄驗證失敗後,您應該按照您在newedit操作中所做的相同方式進行設置。

+0

我缺少'@ categories'和'@ units'。謝謝。 – Robert