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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

594閲覧

ActionController::UrlGenerationError in Comments#new

moto12

総合スコア15

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/06 04:20

編集2020/11/06 05:26

投稿されたコメント(comment)に対して共感(agree)ボタンを押したときエラーが出ました。

( 実際の画像 https://gyazo.com/4ed6810b0da66868150129870face223

おそらくcomment_agree_path(comment, @agree) の@agreeに値が入っていないことが問題だと思うのですが、commentsコントローラーのnewアクションで@agree = Agree.find(params[:agree_id])と定義しても変化はありませんでした。何が問題なのでしょうか?

エラーメッセージは以下の通りです。

【エラーメッセージ画面 (Gyazo)】
https://gyazo.com/6a1492037b3cd20561c4c217e30c098a

発生している問題・エラーメッセージ

エラーメッセージ ActiveRecord::RecordNotFound in CommentsController#new Couldn't find Agree without an ID

該当のソースコード

Ruby

1【news.html.erb 共感ボタンのあるページ 】 2 3<div class="comment"> 4 <div class="comment-box"> 5 <%= form_with model: [@comment, @messages],url:message_comments_path(@message),local: true do |f| %> 6 <%= f.text_area :text, placeholder: "質問する", class: "comment-text" %> 7 <%= hidden_field_tag :message_id,@message.id %> 8 <p class="comment-warn"> 9 <p>他の生徒の質問で回答できる場合には積極的に回答してください!</p> 10 </p> 11 <br> 12 <%= f.submit "質問する",class: "comment-btn"%> 13 <% end %> 14 </div> 15 16<div class="comment-box2"> 17 <h4 class="comment-list"><質問一覧></h4> 18 <% if @comments %> 19 <% @comments.each do |comment| %> 20 <div class="comment-content"> 21 <% if current_user.genre == "教員" %> 22 <p> 23 <strong><%= link_to comment.user.name, "#", class: "comment-nickname" %></strong> 24 </p> 25 <% elsif current_user.genre == "生徒" %> 26 <strong class= "comment-nickname">匿名</strong> 27 <% end %> 28 <p class="comment-texts"> 29 <%= comment.text%> 30 </p> 31 <div class="favorite-btn"> 32 <% if user_signed_in? && current_user.already_agreed?(comment) %> 33 <%= link_to comment_agree_path(comment, @agree), method: :delete do %> 34 <p class="agree-btn-color">共感</p> 35 <% end %> 36 <% elsif user_signed_in? && current_user.id == comment.user_id%> 37 <p class="agree-btn">共感</p> 38 <% else %> 39 <%= link_to comment_agrees_path(comment), method: :post do %> 40 <p class="agree-btn">共感</p> 41 <% end %> 42 <% end %> 43 <p class="agree-count"><%= @comment.agrees.count%>人</p> 44 </div> 45 <ul class="comment-time"> 46 <p class="time"><%= comment.created_at.to_s(:datetime_jp) %></p> 47 <p class="time"><%= comment.created_at.to_s(:datetime_jp2) %></p> 48 </ul> 49 <% if current_user.genre == "教員" %> 50 <p> 51 <%= link_to message_comment_path(@message.id, comment.id), method: :delete, class: "delete-button" do %> 52 <i class="fas fa-trash"></i> 53 <% end %> 54 </p> 55 <% elsif user_signed_in? && current_user.id == comment.user_id %> 56 <p> 57 <%= link_to message_comment_path(@message.id, comment.id), method: :delete, class: "delete-button" do %> 58 <i class="fas fa-trash"></i> 59 <% end %> 60 </p> 61 <% end %> 62 <p> 63 </div> 64 <% end %> 65 <% end %> 66 </div> 67 </div> 68 <p class="back-to-top2"><%=link_to '戻る', root_path %></p> 69

Ruby

1class AgreesController < ApplicationController 2 3 def create 4 @agree = current_user.agrees.create(comment_id: params[:comment_id]) 5 redirect_back(fallback_location: root_path) 6 end 7 8 def destroy 9 @comment = Comment.find(params[:comment_id]) 10 @agree = current_user.agrees.find_by(comment_id: @comment.id) 11 @agree.destroy 12 redirect_back(fallback_location: root_path) 13 end 14 15 private 16 17 def agree_params 18 params.require(:agree).permit( 19 :comment_id, 20 :user_id 21 ).merge(user_id: current_user.id, comment_id: params[:comment_id]) 22 end 23 24end 25

Ruby

1class CommentsController < ApplicationController 2 3 def index 4 @messages = Message.all 5 @room = Room.all 6 @message = Message.find(params[:id]) 7 8 end 9 10 def new 11 @message = Message.find(params[:message_id]) 12 @comments = @message.comments.order("created_at DESC") 13 @comment = Comment.new 14 @messages = Message.all 15 @room = Room.all 16 @agree = Agree.find(params[:id]) 17 end 18 19 def create 20 @message = Message.find(params[:message_id]) 21 @comment = @message.comments.new(comment_params) 22 if @comment.save 23 redirect_to new_message_comment_path(@message) 24 else 25 redirect_to new_message_comment_path(@message) 26 end 27 end 28 29 def destroy 30 @comment = Comment.find(params[:id]) 31 @message = @comment.message 32 if @comment.destroy 33 redirect_to new_message_comment_path(@message) 34 else 35 @comments = @message.comments.order("created_at DESC") 36 render :new 37 end 38 end 39 40 private 41 def comment_params 42 params.require(:message).permit(:text).merge(user_id: current_user.id, message_id: params[:message_id]) 43 end 44end 45

Ruby

1【routes.rb】 2 3Rails.application.routes.draw do 4 devise_for :users 5 get 'messages/index' 6 get 'comments/index' 7 8 root to: "rooms#index" 9 resources :users 10 resources :rooms do 11 resources :messages 12 end 13 resources :messages do 14 resources :comments 15end 16 17 resources :comments do 18 resources :agrees, only: [:create, :destroy] 19end 20 21end 22

Ruby

1【agree.rb】 2 3class Agree < ApplicationRecord 4 belongs_to :user 5 belongs_to :comment 6 7 validates_uniqueness_of :comment_id, scope: :user_id 8end

Ruby

1comment_agrees POST /comments/:comment_id/agrees(.:format) agrees#create 2comment_agree DELETE /comments/:comment_id/agrees/:id(.:format) agrees#destroy

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

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

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

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

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

guest

回答1

0

自己解決

Ruby

1new.html.erb(共感ボタンのあるファイル)】 2下記の2行目をbefore→afterに変更したところうまくいきました。 3 4(before) 5<%= link_to comment_agree_path(comment, @agree), method: :delete do %> 6 7(after) 8<%= link_to comment_agree_path(comment, comment.agrees), method: :delete do %>

投稿2020/11/06 05:26

moto12

総合スコア15

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問