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

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

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

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

Ruby

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

Ruby on Rails

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

Q&A

解決済

1回答

1058閲覧

rails コメント機能のバリデーション時のActionController::UrlGenerationError

aku424tt

総合スコア15

Ruby on Rails 5

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

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/09/11 16:00

編集2020/09/12 01:57

コメント機能をつけて、そのコメント機能にバリデーション機能を付けようとしています。

空白と200文字オーバーのコメント投稿に対してバリデーションをしたいです。
自分の知識とサイトの記事を参考に記述したのですがうまく機能しません。

バリデーションにかかるコメント投稿をすると

ActionController::UrlGenerationError in PostComments#create
Showing /home/vagrant/work/port1/app/views/posts/show.erb where line #66 raised:

No route matches {:action=>"destroy", :controller=>"post_comments", :post_id=>27}, missing required keys: [:id]

このようなエラーになってしまいます。
コメント削除のボタンがエラーの該当箇所になっています。

すでに投稿されているコメントがうまく表示ができなくて、エラーになっているのだと思われます。
すでにコメント投稿されていなければうまく実装できます。また、コメント削除ボタンを消してもうまく実装できます。

ちなみに該当箇所はhtmlの下から16行目あたりです。

イメージ説明

ruby

1class PostsController < ApplicationController 2 before_action :authenticate_user 3 before_action :ensure_correct_user,{only:[:edit, :update]} 4 def new 5 @user = current_user 6 @post = Post.new 7 end 8 def create 9 @user = current_user 10 @post = Post.new(post_params) 11 @post.user_id = current_user.id 12 if @post.save 13 redirect_to post_path(@post.id),notice: "Successful posting!!" 14 else 15 render :new 16 end 17 end 18 def index 19 @user = current_user 20 @posts = Post.all.order(id: "DESC") 21 end 22 def show 23 @post = Post.find(params[:id]) 24 @user = @post.user 25 @comments = @post.post_comments 26 @post_comment_new = PostComment.new 27 end 28 def edit 29 @user = current_user 30 @post = Post.find(params[:id]) 31 end 32 def update 33 @user = current_user 34 @post = Post.find(params[:id]) 35 @post.user = current_user 36 if @post.update(post_params) 37 redirect_to post_path(@post.id),notice: "Successful editing!!" 38 else 39 render :edit 40 end 41 end 42 def destroy 43 @post = Post.find(params[:id]) 44 @post.destroy 45 redirect_to posts_path,notice: "Successful deleting!!" 46 end 47 def ensure_correct_user 48 @post = Post.find(params[:id]) 49 if @post.user_id != current_user.id 50 redirect_to post_path(@post.id) 51 end 52 end 53 private 54 def post_params 55 params.require(:post).permit(:title, :body, :image, :user_id) 56 end 57end 58

ruby

1class PostCommentsController < ApplicationController 2 def create 3 @post = Post.find(params[:post_id]) 4 @post_comment_new = PostComment.new(post_comment_params) 5 @user = @post.user 6 @comments = @post.post_comments 7 @post_comment_new.user_id = current_user.id 8 @post_comment_new.post_id = @post.id 9 if @post_comment_new.save 10 redirect_to post_path(@post.id),notice: "Successful comment posting!!" 11 else 12 flash[:alert] = "Posting error!!" 13 render template: 'posts/show' 14 end 15 end 16 def destroy 17 @comment = PostComment.find(params[:post_id]) 18 @comment.destroy 19 redirect_to request.referer,notice: "Successful deleting!!" 20 end 21 private 22 def post_comment_params 23 params.require(:post_comment).permit(:comment) 24 end 25end 26

html

1<% if flash[:notice] %> 2 <div class="success_message"> 3 <p><%=flash[:notice] %></p> 4 </div> 5 <% elsif flash[:alert]%> 6 <div class="error_message"> 7 <p><%=flash[:arlrt] %></p> 8 </div> 9<% end %> 10<%= render "layouts/prof", user:@user %> 11<div class="post_show"> 12 <!-- このuserの詳細画面に推移 --> 13 <div class="post_show_image_space"> 14 <%= attachment_image_tag @post, :image, class: "post_show_image" %> 15 </div> 16 <div class="post_show_text_area"> 17 <h3>Title</h3> 18 <p><%= @post.title %></p> 19 <h3>Body</h3> 20 <p><%= @post.body %></p> 21 </div> 22 <div class="post_show_text_area"> 23 <h3>Favorites</h3> 24 <!-- favorites --> 25 <p> 26 <% if @post.favorited_by?(current_user) %> 27 <%= link_to post_favorites_path(@post), method: :delete do%> 28 <i class="fas fa-heart" style="color:red;"></i> 29 <% end %> 30 <% else %> 31 <%= link_to post_favorites_path(@post),method: :post do%> 32 <i class="far fa-heart"></i> 33 <% end %> 34 <% end %> <%=@post.favorites.count%> favorites</p> 35 <h3>Comments</h3> 36 <%= render "layouts/flash", object:@post_comment_new%> 37 <h4>Post a comment</h4> 38 <%= form_for [@post,@post_comment_new] do |f|%> 39 <%=f.text_area :comment , placeholder: "comment"%> 40 <%=f.submit "submit"%> 41 <% end %> 42 <!-- クリックされたらこの表示が消えてコメントが全部表示される --> 43 <a class="posted_comments comment_close open">See all <%=@post.post_comments.count%> comments</a> 44 <div class="posted_comments comment_open"> 45 <a>Fade out comments</a> 46 <table class="table"> 47 <thead> 48 <tr> 49 <th>Commented user</th> 50 <th>Comment</th> 51 <th colspan="1"></th> 52 </tr> 53 </thead> 54   <%@comments.reverse. each do |comment|%> 55 <tbody> 56 <tr> 57 <td> 58 <%= link_to user_path(comment.user_id) do %> 59 <%= attachment_image_tag comment.user, :profile_image, size: "60x60", fallback: "no_image.jpg" , class: "img-circle" %> 60 <%=comment.user.name %> 61 <% end %> 62 </td> 63 <td><%=comment.comment%></td> 64 <td> 65 <% if comment.user_id == current_user.id %> 66 <%= link_to "comment detlate", post_post_comment_path(comment.id), method: :delete, data: {confirm: "削除しますか?"},class: "btn btn-danger" %> 67 <% end %> 68 </td> 69 </tr> 70 </tbody> 71   <% end %> 72 </table> 73 </div> 74 </div> 75 <div class="post_show_path"> 76 <% if @user == current_user %> 77 <%= link_to "post edit",edit_post_path(@post.id),class: "btn btn-success " %> 78 <%= link_to "post detlate",post_path(@post.id), method: :delete, data: {confirm: "削除しますか?"},class: "btn btn-danger" %> 79 <% end %> 80 </div> 81</div> 82

ruby

1class PostComment < ApplicationRecord 2 belongs_to :user 3 belongs_to :post 4 validates :comment, presence: true, length: { maximum: 50} 5end 6

ruby

1Rails.application.routes.draw do 2 devise_for :users 3 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 4 root 'homes#top' 5 resources :users 6 resources :posts do 7 resource :favorites, only: [:create, :destroy] 8 resources :post_comments, only: [:create, :destroy] 9 end 10end 11

不要な点がありましたらお申し付けください。
分かる方がいらっしゃいましたらぜひよろしくお願いします。

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

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

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

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

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

winterboum

2020/09/11 22:02

config/routes.rbを載せてください
aku424tt

2020/09/12 01:57

ご指摘ありがとうございます。 追加させていただきました
guest

回答1

0

ベストアンサー

PostComment を指定したパスが必要だと思いました。

ruby

1# <%= link_to "comment detlate", post_post_comment_path(comment.id), method: :delete, data: {confirm: "削除しますか?"},class: "btn btn-danger" %> 2<%= link_to "comment detlate", delete_post_comment_path(@post, comment), method: :delete, data: {confirm: "削除しますか?"},class: "btn btn-danger" %>

投稿2020/09/12 02:17

unhappychoice

総合スコア1531

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

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

aku424tt

2020/09/12 03:53

ありがとうございました。解決できました。 もしお時間がありましたら、どうしてこのような記述になるのか教えていただけますでしょうか?
unhappychoice

2020/09/12 04:01

`bundle exec rails routes` を実行してルートを表示すると、 `DELETE /posts/:post_id/post_comments/:id` のような表示があるはずで、`:post_id` と `:comment_id` を指定する必要があるため、で伝わるでしょうかmm
aku424tt

2020/09/12 05:00

なんとなくは理解できました。コメントだけではなく、そのコメントがどこの投稿にされたコメントなのかを示す必要があるということですか?
unhappychoice

2020/09/12 05:05

その理解で良さそうです。 が、やりたいこと次第というか、本質的には post_comment は id だけで特定できるはずなので - resources :post_comments, only: [:create, :destroy] を resources :posts の外に出す - DELETE /post_comments/:id がルートとして生成される - delete_post_comment_path(comment) と記述する でも同じことが出来ますmm
aku424tt

2020/09/12 05:27

ありがとうございます。 今回に関してはルーティングをネストしているため、その2つを書く必要があるのであって、ネストしていなければ、別の記述ができるということで間違い無いでしょうか
aku424tt

2020/09/12 06:01

本当に詳しく教えていただきありがとうございます。 とても勉強になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問