前提・実現したいこと
部屋の中で投稿されたメッセージを削除しようと、削除ボタンを押すと、メッセージは削除される
(他のメッセージも3つくらい同時に削除されてしまう...)のですが
下記のようなエラーメッセージが出てきます。
【削除ボタン画面のイメージ】
発生している問題・エラーメッセージ
該当のソースコード
Ruby
1class MessagesController < ApplicationController 2 def index 3 @comment = Comment.new 4 @message = Message.new 5 @room = Room.find(params[:room_id]) 6 @messages = @room.messages.includes(:user).order("created_at DESC") 7 @users = @room.users.order("student_number ASC") 8 end 9 10 def new 11 @room = Room.find(params[:room_id]) 12 @comment = Comment.new 13 @message = Message.new 14 end 15 16 def show 17 @comment = Comment.new 18 @comments = @message.comments.includes(:user) 19 end 20 21 def create 22 @room = Room.find(params[:id]) 23 @message = @room.messages.new(message_params) 24 if @message.save 25 redirect_to room_messages_path(@room) 26 else 27 redirect_to room_messages_path(@room) 28 end 29 end 30 31 def destroy 32 @room = Room.find(params[:id]) 33 @messages = @room.messages 34 @messages.each do |message| 35 if message.destroy 36 redirect_to room_messages_path(message, message.room) 37 else 38 render :new 39 end 40 end 41 end 42 43 private 44 45 def message_params 46 params.require(:message).permit(:content, :image).merge(user_id: current_user.id) 47 end 48end 49
Ruby
1【削除実行後のログ】 2 3Started DELETE "/rooms/131/messages/1" for ::1 at 2020-11-08 11:25:46 +0900 4Processing by MessagesController#destroy as HTML 5 Parameters: {"authenticity_token"=>"auEO4Gz8UK3Eyf8lFt/iqxVzsU1pL2UgBOYPV8+vyftXr6GzYYHj18L4GReMzXEPzlrDslt9QerySbS80Ot4iA==", "room_id"=>"131", "id"=>"1"} 6 User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1 7 Room Load (0.2ms) SELECT `rooms`.* FROM `rooms` WHERE `rooms`.`id` = 1 LIMIT 1 8 ↳ app/controllers/messages_controller.rb:32:in `destroy' 9 Message Load (0.2ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`room_id` = 1 10 ↳ app/controllers/messages_controller.rb:34:in `destroy' 11 (0.1ms) BEGIN 12 ↳ app/controllers/messages_controller.rb:35:in `block in destroy' 13 ActiveStorage::Attachment Load (0.3ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 131 AND `active_storage_attachments`.`record_type` = 'Message' AND `active_storage_attachments`.`name` = 'image' LIMIT 1 14 ↳ app/controllers/messages_controller.rb:35:in `block in destroy' 15 Message Destroy (0.2ms) DELETE FROM `messages` WHERE `messages`.`id` = 131 16 ↳ app/controllers/messages_controller.rb:35:in `block in destroy' 17 (0.5ms) COMMIT 18 ↳ app/controllers/messages_controller.rb:35:in `block in destroy' 19Redirected to http://localhost:3000/rooms/131/messages/1 20 (0.1ms) BEGIN 21 ↳ app/controllers/messages_controller.rb:35:in `block in destroy' 22 ActiveStorage::Attachment Load (0.3ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 132 AND `active_storage_attachments`.`record_type` = 'Message' AND `active_storage_attachments`.`name` = 'image' LIMIT 1 23 ↳ app/controllers/messages_controller.rb:35:in `block in destroy' 24 Message Destroy (0.2ms) DELETE FROM `messages` WHERE `messages`.`id` = 132 25 ↳ app/controllers/messages_controller.rb:35:in `block in destroy' 26 (0.2ms) COMMIT 27 ↳ app/controllers/messages_controller.rb:35:in `block in destroy' 28Redirected to 29Completed 500 Internal Server Error in 15ms (ActiveRecord: 2.7ms | Allocations: 11677) 30 31 32AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".): 33
Ruby
1【routes.rb】 2 3Rails.application.routes.draw do 4 devise_for :users 5 get 'messages/index' 6 get 'comments/index' 7 get 'responses/create' => 'responses#create' 8 root to: "rooms#index" 9 resources :users 10 resources :rooms do 11 resources :messages 12 end 13 14 resources :messages do 15 resources :comments 16 end 17 18 resources :comments do 19 resources :agrees, only: [:create, :destroy] 20 end 21 22 resources :comments do 23 resources :responses, only: [:new, :create, :destroy] 24 end 25 26end 27
Railsって結構エラーメッセージ懇切丁寧なのですけど、エラーメッセージ読んでませんか?
https://qiita.com/cannorin/items/eb062aae88bfe2ad6fe5