2

I have a problem with simple model validation. Entering materials without SKU is getting me error message:

NoMethodError in Materials#create undefined method `empty?' for nil:NilClass

enter image description here material.rb:

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

materials_controler.rb (create part only):

# 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

2 Answers 2

3

Your @units instance variable is nil in create action. You should set it the same way as you do in new or edit actions after your records fails validation.

Sign up to request clarification or add additional context in comments.

1 Comment

I was missing @categories and @units. Thank you.
1

Define @units also in your create method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.