简体   繁体   English

错误的参数数错误

[英]Wrong number of Arguments error

I'm following a tutorial and building a small instagram clone with Rails 4.2.5 我正在关注教程并使用Rails 4.2.5构建一个小型instagram克隆

But I'm not sure why I keep on getting this Wrong number of arguments error. 但是我不确定为什么我继续收到错误的参数数目错误。

我收到此错误

EDIT: This is error message in a text format, it is present at Line 15: 编辑:这是文本格式的错误消息,它出现在第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. 我在pics_controller中定义了一个私有方法'pic_params',该方法传入了两个参数:title和:description。

And my update action passes in the pic_params function: 我的更新操作通过pic_params函数:

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'! 根据'pic_params'应该指定2个参数!

If anyone can help, that'd be fantastic! 如果有人可以提供帮助,那就太好了!

You are wrong at Pic.update(pic_params) 您在Pic.update(pic_params)上出错

Pic is a model, not object, you only use update with an object. Pic是模型,不是对象,您只能对对象使用update。

Plz try: 请尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM