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

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

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

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

1395閲覧

Double Render Error

moto12

総合スコア15

Ruby

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/11/08 02:29

前提・実現したいこと

部屋の中で投稿されたメッセージを削除しようと、削除ボタンを押すと、メッセージは削除される
(他のメッセージも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

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

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

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

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

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

guest

回答1

0

エラーメッセージにあるまま、です。
ひとつのアクションでは一回しか宣言できません。
みため if-else-end にわけられて1つにみえますが、@messages.each の中にあるので、@messages.size回宣言されています。

投稿2020/11/08 03:27

winterboum

総合スコア23329

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問