【发布时间】:2017-11-07 20:27:02
【问题描述】:
我尝试添加更新功能,但遇到以下错误。
未定义的局部变量或方法 `pic' for # 你的意思是? @pic
提取的源代码(第 30 行附近):
28 end 29 def update 30 if @pic.update(pic.params) 31 redirect_to @pic, notice: "Updated!!" 32 else 33 render 'edit'
我的pics_controller.rb文件如下
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 new
@pic = Pic.new
end
def create
@pic = Pic.new(pic_params)
if @pic.save
redirect_to @pic,notice: "Yesss! It was posted!"
else
render 'new'
end
end
def edit
end
def update
if @pic.update(pic.params)
redirect_to @pic, notice: "Updated!!"
else
render 'edit'
end
end
private
def pic_params
params.require(:pic).permit(:title, :description)
end
def find_pic
@pic = Pic.find(params[:id])
end
end
我该如何解决这个问题?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-5 ruby-on-rails-5.1