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

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

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

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

Ruby

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

Ruby on Rails

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

Q&A

1回答

1815閲覧

【Ruby on Rails】コメント削除が出来ない

is02

総合スコア17

Ruby on Rails 5

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

Ruby

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

Ruby on Rails

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

0グッド

1クリップ

投稿2020/02/10 03:46

編集2020/02/10 03:47

前提・実現したいこと

コメント削除機能をつけたい

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

イメージ説明

該当のソースコード

####モデル
user.rb

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :post_images, dependent: :destroy has_many :favorites, dependent: :destroy has_many :fav_post_images, through: :favorites, source: :post_image has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_post_images, through: :favorites, source: :post_image has_many :post_comments, dependent: :destroy has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverse_of_relationships, source: :user # 2~8文字以内で名前が入っているかの確認 validates :name, presence: true, length: { in: 2..8 }, uniqueness: true # 自己紹介 100文字以内 validates :introduction, length: { maximum: 100 } # 可能なコスプレ 100文字以内 validates :like_cos, length: { maximum: 100 } # refile定義 attachment :profile_image # フォロー機能のメソッド def follow(other_user) unless self == other_user # フォローしようとしている人が自分自身ではないか # フォローしようとしている人が自分以外ならフォローする self.relationships.find_or_create_by(follow_id: other_user.id) end end def unfollow(other_user) relationship = self.relationships.find_by(follow_id: other_user.id) relationship.destroy if relationship # フォローしていたらアンフォローする end def following?(other_user) self.followings.include?(other_user) # other_userが含まれていたらtrueを返す end end

post_image.rb

class PostImage < ApplicationRecord belongs_to :user has_many :favorites, dependent: :destroy has_many :fav_users, through: :favorites, source: :user has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_users, through: :cosplay_favorites, source: :user has_many :post_comments, dependent: :destroy # refile定義 attachment :real_image attachment :cosplay_image validates :real_image, presence: true validates :cosplay_image, presence: true # 投稿を降順に並び替えし、最新のものを上にくるようにする default_scope -> { order(created_at: :asc) } end

post_comment.rb

class PostComment < ApplicationRecord belongs_to :user belongs_to :post_image # コメントが空白の時はエラー表示させる validates :comment, presence: true end

####コントローラー
users_controller.rb

class UsersController < ApplicationController def show @user = User.find(params[:id]) @post_images = @user.post_images end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if @user.update(user_params) redirect_to user_path(@user.id) else render 'edit' end end def following @user = User.find(params[:id]) @users = @user.followings render 'show_follow' end def followers @user = User.find(params[:id]) @users = @user.followers render 'show_follower' end private def user_params params.require(:user).permit(:name, :profile_image, :introduction, :like_cos) end end

post_images_controller.rb

class PostImagesController < ApplicationController def new @post_image = PostImage.new end # 投稿データの保存 def create @post_image = PostImage.new(post_image_params) @post_image.user_id = current_user.id if @post_image.save redirect_to post_images_path else render 'new' end end def index @post_images = PostImage.page(params[:page]).reverse_order end def show @post_image = PostImage.find(params[:id]) @post_comment = PostComment.new end def destroy @post_image = PostImage.find(params[:id]) @post_image.destroy redirect_to post_images_path end private # 投稿データのストロングパラメータ def post_image_params params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption, :favorites_count) end end

post_comments_controller.rb

class PostCommentsController < ApplicationController def create @post_image = PostImage.find(params[:post_image_id]) @post_comment = current_user.post_comments.new(post_comment_params) @post_comment.post_image_id = @post_image.id if @post_comment.save redirect_to post_image_path(@post_image) else render 'post_images/show' end end def destroy @post_image = PostImage.find(params[:post_image_id]) @post_comment = current_user.post_comments.find(params[:id]) @post_comment.post_image_id = @post_image.id @post_comment.destroy redirect_to post_image_path(@post_image) end private def post_comment_params params.require(:post_comment).permit(:user_id, :post_image_id, :comment) end end

####ビュー
post_images/show.html.erb

<div class="header"> <nav class="navigation"> <img src="/assets/logo.png"> <ul> <li> <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> </li> <li> <%= link_to '投稿一覧', root_path %> </li> <li> <%= link_to 'マイページ', user_path(current_user.id) %> </li> </ul> </nav> </div> <div class="post_images_show_wrapper"> <div class="index_box"> <div class="post_images_index_user"> <ul> <li> <%= attachment_image_tag @user, :profile_image, fallback: "no_image.jpg" %> </li> <li><p><%= link_to "#{@post_image.user.name}", user_path(@post_image.user) %></p></li> </ul> </div> <div class="post_images_index_title"> <div class="image_title"> <h2>Real</h2> </div> <div class="image_title"> <h2>Cosplay</h2> </div> </div> <div class="post_images_box"> <div class="post_image"> <%= attachment_image_tag @post_image, :real_image %> </div> <div class="post_image"> <%= attachment_image_tag @post_image, :cosplay_image %> </div> </div> <div class="image_name"> <%= @post_image.real_image_name %> </div> <div class="image_name"> <%= @post_image.cosplay_image_name %> </div> <div class="favorites_area"> <div class="favorite_area"> <%= render partial: 'post_images/post_images', locals: { post_image: @post_image } %> </div> <div class="favorite_area"> <%= render partial: 'post_images/cosplay_post_images', locals: { post_image: @post_image } %> </div> </div> <div class="image_caption"> <ul> <li> <%= attachment_image_tag @user, :profile_image, fallback: "no_image.jpg" %> </li> <li> <p><%= @post_image.user.name %> </p> </li> <li> <p><%= @post_image.post_comments.count %>件のコメント</p> </li> <li> <% if @post_image.user == current_user %> <p class="post_delete"> <%= link_to "投稿を削除", post_image_path(@post_image), method: :delete, data: { confirm: '投稿を削除しますか?' } %></p> <% end %> </li> </ul> <span class="caption"><%= @post_image.caption %></span> </div> <p class="comment_title">みんなのコメント</p> <div class="comment_area"> <% @post_image.post_comments.each do |post_comment| %> <div class="comment_box"> <div class="comment_user"> <span class="date"><%= post_comment.created_at.strftime('%Y/%m/%d') %> </span> <span class="user"><%= post_comment.user.name %> : </span> </div> <div class="comment_text"> <%= post_comment.comment %> <p class="post_delete"> <%= link_to "削除", post_image_post_comments_path(@post_image), method: :delete, data: { confirm: 'コメントを削除しますか?' } %></p> </div> </div> <% end %> </div> <% if @post_comment.errors.any? %> <% @post_comment.errors.full_messages.each do |message| %> <li class="edit_error"><%= message %></li> <% end %> <% end %> <div class="comment_type"> <%= form_for [@post_image, @post_comment] do |f| %> <ul> <li><%= f.text_area :comment, placeholder: "コメントを入力してください" %></li> <li><%= f.submit "送信する" %></li> </ul> <% end %> </div> </div> </div>

補足情報(FW/ツールのバージョンなど)

ruby 2.5.7p206
Rails 5.2.4.1

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

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

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

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

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

guest

回答1

0

has_many :post_comments, dependent: :destroy
となっているので、 post_imageだけdestroyすればOKです。

載っているcodeの場合エラーとなったのは、link_toで送っているのが post_image のidだけなので、params[:id] はnilになり、掲示のエラーとなります

投稿2020/02/10 04:03

winterboum

総合スコア23347

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

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

is02

2020/02/10 05:24 編集

回答ありがとうございます。 post_image_post_comments_path(@post_image,post_comment)としてみたんですが、同じエラーが出てしまいます。 また、has_many :post_comments, dependent: :destroyのdependent: :destroyは削除してもいいということなんでしょうか。
winterboum

2020/02/10 06:12

いえ、そちらは残しましょう。 > @post_comment = current_user.post_comments.find(params[:id]) が不要です
is02

2020/02/10 06:49

@post_imageをdestroyしてしまうとコメントは確かに消えるのですが、投稿写真も消えてしまいます。 投稿写真は残したまま、削除したいコメントだけを消すというのは可能なんでしょうか。
winterboum

2020/02/10 06:52

ん? post_imageを削除するということは、imageすなわち写真を削除するということではない?
is02

2020/02/10 06:54

うまく伝わっていなくて申し訳ありません。 投稿した写真は残して、指定したコメントだけを削除したいです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問