Wali Chaudhary
Wali Chaudhary

Reputation: 177

Wrong number of Arguments error

I'm following a tutorial and building a small instagram clone with Rails 4.2.5

But I'm not sure why I keep on getting this Wrong number of arguments error.

I am receiving this error

EDIT: This is error message in a text format, it is present at Line 15:

13
14    def update
15        if @pic = Pic.update(pic_params)
16           redirect_to @pic, notice: "Congrats! Your picture was updated!"
17        else
18           render 'edit'
19        end
20    end

I have defined a private method 'pic_params' in my pics_controller that passes in 2 arguments, :title and :description.

And my update action passes in the pic_params function:

class PicsController < ApplicationController
before_action :find_pic, only: [:show, :edit, :update, :destroy]

def index
    @pics = Pic.all.order("created_at DESC")
end

def show
end

def edit
end

def update
    if @pic = Pic.update(pic_params)
        redirect_to @pic, notice: "Congrats! Your picture was updated!"
    else
        render 'edit'
    end
end

def new
    @pic = Pic.new
end

def create
    @pic = Pic.new(pic_params)
    if @pic.save
        redirect_to @pic, notice: "Yess! It worked!"
    else
        render 'new'
    end
end


private

def pic_params
    params.require(:pic).permit(:title, :description)
end

def find_pic
   @pic = Pic.find(params[:id])
end

end

And I know for sure my model also includes both of those columns..(title and description) according to my schema.

ActiveRecord::Schema.define(version: 20161218131012) do

create_table "pics", force: :cascade do |t|
 t.string   "title"
 t.text     "description"
 t.datetime "created_at",  null: false
 t.datetime "updated_at",  null: false

end

end

So why am I getting this error? There should be 2 arguments specified according to 'pic_params'!

If anyone can help, that'd be fantastic!

Upvotes: 0

Views: 3140

Answers (1)

Duyet Nguyen
Duyet Nguyen

Reputation: 543

You are wrong at Pic.update(pic_params)

Pic is a model, not object, you only use update with an object.

Plz try:

def update
  if @pic.update(pic_params)
    redirect_to @pic, notice: "Congrats! Your picture was updated!"
  else
    render 'edit'
  end
end

Upvotes: 3

Related Questions