Home > Mobile >  undefined method `save' for nil:NilClass for my app
undefined method `save' for nil:NilClass for my app

Time:06-01

[enter image description here][1]

I keep getting this error message and can not figure out why it is. I believe it is because @resturant is nil. But I do not understand why it is nil. Is there anyway to 'print or console log' the response so I can see what the problem is [1]: https://i.stack.imgur.com/sHUrz.png

Here is the model

    class Resturant < ApplicationRecord
    mount_uploader :image, ImageUploader
    serialize :image, JSON # If you use SQLite, add this line
    belongs_to :user, optional: true

    validates :name, :description, :location, :resturant, :glutenfree, :vegan, presence: true
    validates :description, length: {maximum: 1000, too_long: "%{count} characters is the maximum allowed"}

    validates :title, length: {maximum: 140, too_long: "%{count} characters is the maximum allowed"}
end

here is the controller:

class Resturant < ApplicationRecord
    mount_uploader :image, ImageUploader
    serialize :image, JSON # If you use SQLite, add this line
    belongs_to :user, optional: true

    validates :name, :description, :location, :resturant, :glutenfree, :vegan, presence: true
    validates :description, length: {maximum: 1000, too_long: "%{count} characters is the maximum allowed"}

    validates :title, length: {maximum: 140, too_long: "%{count} characters is the maximum allowed"}

end

# GET /resturants/new
def new
  @resturant = Resturant.new
end

# GET /resturants/1/edit
def edit
end

# POST /resturants or /resturants.json
def create
  @resturants = Resturant.new(resturant_params)

  respond_to do |format|
    
    if @resturant.save
      format.html { redirect_to resturant_url(@resturant), notice: "Resturant was successfully created." }
      format.json { render :show, status: :created, location: @resturant }
    else
      format.html { render :new, status: :unprocessable_entity }
      format.json { render json: @resturant.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /resturants/1 or /resturants/1.json
def update
  respond_to do |format|

    if @resturant.update(resturant_params)
      format.html { redirect_to resturant_url(@resturant), notice: "Resturant was successfully updated." }
      format.json { render :show, status: :ok, location: @resturant }
    else
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @resturant.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /resturants/1 or /resturants/1.json
def destroy
  @resturant.destroy

  respond_to do |format|
    format.html { redirect_to resturants_url, notice: "Resturant was successfully destroyed." }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_resturant
    @resturant = Resturant.find(params[:id])
  end

  # Only allow a list of trusted parameters through.
  def resturant_params
    params.require(:resturant).permit(:name, :genre, :description, :location, :glutenfree, :vegan, :image, :resturant)
  end

end

CodePudding user response:

You have a typo. (It often takes a second set of fresh eyes to see these.)
You have accidentally pluralized resturant in your create action but then are using the singular later on.

You have:

@resturants = Resturant.new(resturant_params)

Change it to:

@resturant = Resturant.new(resturant_params)

(You are also misspelling "Restaurant" throughout your entire application. It is up to you if you'd like to fix that or not. I know I myself have trouble spelling that one for some silly reason.)

  • Related