質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

2回答

1639閲覧

Ruby on Rails にて写真アプリのDelete時のエラー

Tomoaki_Fukuda

総合スコア75

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2016/01/23 07:35

編集2016/01/24 05:28

###前提・実現したいこと
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 %>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jinco

2016/01/23 10:56

deleteを呼び出すviewを見せていただけないでしょうか。
Tomoaki_Fukuda

2016/01/24 02:07

ご返信ありがとうございます。 deleteを呼び出すviewである「index.html.erb」を追記情報に記載させて頂きました。
guest

回答2

0

ベストアンサー

def destroy @photo.destroy respond_to do |format| format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' } format.json { head :no_content } end end

ここのformat.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' }これはindexにリダイレクトされるんだと思いますが、before_action :set_current_user_photo, only: [:edit, :update, :destroy]でonly指定しているのに「set_current_user_photo」メソッドが呼び出されていることが問題ではないかと思います。

rails sコマンドで画面を確認しているなら、そのログを、powderなど利用されているならターミナルを開いてtail -f log/development.logを実行すし、ログを見て下さい。
意図しないパスが読まれているのではないでしょうか?
ログを見てみたいです。

また、「better_errors」と言うgemがありますので、これを入れておくとエラー画面上で確認できる情報が増えて便利です。
(参考)https://github.com/charliesome/better_errors

最後に恐らくですが、index.html.erbが違うページのものではないかと思います。
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
コメント用ではないでしょうか?

投稿2016/01/24 11:39

TakamichiHara

総合スコア27

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

エラーの把握

今回発生しているエラーは下記のエラーですね。

Couldn't find Photo with 'id'=1 [WHERE "photo"."user_id"=?]

コードのエラー該当箇所は以下になります。

def set_current_user_photo @photo = current_user.photos.find(params[:id]) end

ここで、エラーメッセージを見てみると、
「user_idがログインしているユーザーのid(current_userのid)で、idがparams:idのレコードをPhotosテーブルで探したけど見つけることが出来なかった」
と言われています。
つまり、Photosテーブルに、user_id = current_user.id かつ id = params[:id] のレコードがなかったことがエラーの原因になります。

対応

おそらく、削除はそのphotoを作成したユーザーのみが削除出来るようにしたいと思いますので、

@photo = current_user.photos.find(params[:id])

のコードの前段階でcurrent_userがそのphotoの作成者でなければ他のページにリダイレクトするようにすれば良いと思います。その際、フラッシュメッセージ等を入れてあげるとさらによいかと思います。

投稿2016/01/24 06:02

seishin

総合スコア10

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Tomoaki_Fukuda

2016/01/24 06:21

seishinさん、ご丁寧なご回答ありがとうございました。 もし可能であれば、「current_userがそのphotoの作成者でなければ他のページにリダイレクトする」コードをご教示頂くことは出来ませんでしょうか?
seishin

2016/01/25 16:28 編集

@photo = Photo.find(params[:id]) unless @photo.user_id == current_user.id redirect_to root_path end とかどうでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問