###前提・実現したいこと
Ruby on Rails にて写真アプリを作成中です。
写真を「Delete」しようとすると下記のエラーメッセージが表示されてしまいます。
どのように対処したらよろしいでしょうか?
###発生している問題・エラーメッセージ
https://gyazo.com/21705ed118a90aaf65dd127e348f4a2f
###ソースコード 「photos_controller.rb」
ruby
1class PhotosController < ApplicationController 2 before_action :login_check, only: [:new, :edit, :update, :destroy] 3 4 before_action :set_current_user_photo, only: [:edit, :update, :destroy] 5 6 # GET /photos 7 # GET /photos.json 8 def index 9 @photos = Photo.all 10 end 11 12 # GET /photos/1 13 # GET /photos/1.json 14 def show 15 @photo = Photo.includes(:user).find(params[:id]) 16 @comments = @photo.comments.includes(:user).all 17 @comment = @photo.comments.build(user_id: current_user.id) if current_user 18 end 19 20 # GET /photos/new 21 def new 22 @photo = current_user.photos.build 23 end 24 25 # GET /photos/1/edit 26 def edit 27 end 28 29 # POST /photos 30 # POST /photos.json 31 def create 32 @photo = Photo.new(photo_params) 33 34 respond_to do |format| 35 if @photo.save 36 format.html { redirect_to @photo, notice: 'Photo was successfully created.' } 37 format.json { render :show, status: :created, location: @photo } 38 else 39 format.html { render :new } 40 format.json { render json: @photo.errors, status: :unprocessable_entity } 41 end 42 end 43 end 44 45 # PATCH/PUT /photos/1 46 # PATCH/PUT /photos/1.json 47 def update 48 respond_to do |format| 49 if @photo.update(photo_params) 50 format.html { redirect_to @photo, notice: 'Photo was successfully updated.' } 51 format.json { render :show, status: :ok, location: @photo } 52 else 53 format.html { render :edit } 54 format.json { render json: @photo.errors, status: :unprocessable_entity } 55 end 56 end 57 end 58 59 # DELETE /photos/1 60 # DELETE /photos/1.json 61 def destroy 62 @photo.destroy 63 respond_to do |format| 64 format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' } 65 format.json { head :no_content } 66 end 67 end 68 69 private 70 def login_check 71 unless user_signed_in? 72 flash[:alert] = "ログインしてください" 73 redirect_to root_path 74 end 75 end 76 # Use callbacks to share common setup or constraints between actions. 77 def set_current_user_photo 78 @photo = current_user.photos.find(params[:id]) 79 end 80 81 # Never trust parameters from the scary internet, only allow the white list through. 82 def photo_params 83 params.require(:photo).permit(:image, :caption, :user_id) 84 end 85end 86
###追加情報「index.html.erb」
<p id="notice"><%= notice %></p> <h1>Listing Comments</h1> <table> <thead> <tr> <th>User</th> <th>Photo</th> <th>Body</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @comments.each do |comment| %> <tr> <td><%= comment.user_id %></td> <td><%= comment.photo_id %></td> <td><%= comment.body %></td> <td><%= link_to 'Show', comment %></td> <td><%= link_to 'Edit', edit_comment_path(comment) %></td> <td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody> </table> <br> <%= link_to 'New Comment', new_comment_path %>
回答2件
あなたの回答
tips
プレビュー