오류

2017-11-07 5 views
0

액티브 :: RecordNotFound PicsController에서 # 보여 레일에 액티브 :: RecordNotFound오류

'ID'로 그림을 찾을 수 없습니다 = 1

추출 소스 (라인 # 주위 34) :

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

오류가 해결되지 않습니다.

pics_controller.rb은 다음과 같습니다.

class PicsController < ApplicationController 

     before_action :find_pic, only: [:show, :edit, :update, :destroy] 

     def index 
     end 
     def show 
     end 
     def new 
     @pic = Pic.new 
     end 
     def create 
     @pic = current_user.pics.build(pic_params) 
     if @pic.save 
      redirect_to @pic,notice: "Yesss! It was posted!" 
     else 
      render 'new' 
     end 
     end 
     private 
     def pic_params 
     params.require(:pic).permit(:title, :description) 
     end 
     def find_pic 
     @pic = Pic.find(params[:id]).permit(:title) rescue nil 
     end 
    end 

show.html.haml

%h1= @pic.title 
%p= @pic.description 

= link_to "Back", root_path 

업데이트 : 여기 rake routes를 실행하고 출력입니다.

$ rake routes 
    Prefix Verb URI Pattern    Controller#Action 
    pics GET /pics(.:format)   pics#index 
     POST /pics(.:format)   pics#create 
new_pic GET /pics/new(.:format)  pics#new 
edit_pic GET /pics/:id/edit(.:format) pics#edit 
    pic GET /pics/:id(.:format)  pics#show 
     PATCH /pics/:id(.:format)  pics#update 
     PUT /pics/:id(.:format)  pics#update 
     DELETE /pics/:id(.:format)  pics#destroy 
    root GET /      pics#index 
+0

pics' 테이블 – rony36

+0

'에이 기록되지 콘솔을 열고 Pic.find (1)를 시도하면 해당 ID가있는 레코드가 있습니까? – oowowaee

+0

질문에 그림을 게시하지 마십시오. 대신 관련 텍스트를 잘라내어 붙여 넣으십시오. 동일한 문제가있는 다른 사람이 나중에이 질문을 접할 수는 있지만 이미지 호스트가 이미지를 삭제하면 질문이 더 이상 의미가 없을 것입니다. – MarsAtomic

답변

1

충분해야 할 @pic = Pic.find(params[:id])을 시도해야합니다.

%h1= @pic.title if @pic.present? 
%p= @pic.description if @pic.present? 

= link_to "Back", root_path 

사진이없는 경우 오류를 해결하려고 시도하십시오.

+0

Worked !! 감사. –

0

나는이 .permit을 객체에, 그리고 rescue nil 것을 혼란스럽게 생각합니다. 필요한 이유는 무엇입니까?

지금까지 내가 당신이 find_pic 것을 다시 작성해야 그래서 어쩌면, .permitActionController::Parameters 허용됩니다 more details

알고 :

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

허용을 추가하지 않고 다른 오류가 발생했습니다. PicsController # show의 ActiveRecord :: RecordNotFound –