前提・実現したいこと
コメント削除機能をつけたい
発生している問題・エラーメッセージ
該当のソースコード
####モデル
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
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/10 05:24 編集
2020/02/10 06:12
2020/02/10 06:49
2020/02/10 06:52
2020/02/10 06:54