2017-11-07 5 views
2

업데이트 기능을 추가하려고 시도했지만 다음 오류가 발생했습니다.레일스 : PicsController # 이름 오류

로컬 변수가 정의되지 않았거나 메소드에 'pic'이 #있는 것 같습니다. (라인 # 30 정도) @pic

추출 소스 :

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 

가 어떻게이 문제를 해결할 수있는 다음과 같은

 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 파일은?

답변

1

pic 변수가 업데이트 방법의 범위에 없으므로 액세스 할 수 없습니다.

내가 대신 지역 변수라는 그림에 액세스를 시도하고, 당신의 코드를 업데이트하려고 PARAMS 무엇을 당신이 필요로하는 방법 pic_params 생각 :

if @pic.update(pic_params) 
    redirect_to @pic, notice: 'Updated!!' 
else 
    render 'edit' 
end 
+0

YH .. 일했다. –