Reputation: 574
I tried add an Update function and I am encountering following error.
undefined local variable or method `pic' for # Did you mean? @pic
Extracted source (around line #30):
28 end 29 def update 30 if @pic.update(pic.params) 31 redirect_to @pic, notice: "Updated!!" 32 else 33 render 'edit'
My pics_controller.rb file is as follows
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
How can I fix this?
Upvotes: 1
Views: 84
Reputation: 33471
As there's no pic
variable in the scope of the update method, then isn't possible to access to it.
I think what you need is the method pic_params
instead trying to access a local variable called pic and to params, try updating your code to:
if @pic.update(pic_params)
redirect_to @pic, notice: 'Updated!!'
else
render 'edit'
end
Upvotes: 1