前提・実現したいこと
投稿にいいね機能を実装したいと思い
https://qiita.com/nojinoji/items/2c66499848d882c31ffa
このサイトを参考にコードを書いたのですが
詳細ページにアクセスするとエラーが出る
発生している問題・エラーメッセージ
NoMethodError in Comments#show Showing /app/views/comments/show.html.haml where line #15 raised: undefined method `likes' for nil:NilClass
該当のソースコード
routes
1Rails.application.routes.draw do 2 devise_for :users 3 resources :users, only: [:index, :show] 4 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 5 root to: 'posts#index' 6 resources :posts, only: [:index] 7 resources :comments, only: [:index, :new, :create, :show, :destory, :edit] do 8 resources :likes, only: [:create, :destroy] 9 end 10end
model
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 has_many :user_members 7 has_many :members, through: :user_members 8 has_many :comments, dependent: :destroy 9 has_many :likes, dependent: :destroy 10 has_many :liked_comments, through: :likes, source: :post 11 def alreadey_liked?(comment) 12 self.likes.exists?(comment_id: comment.id) 13 end 14end
model
1class Comment < ApplicationRecord 2 belongs_to :user 3 has_many :likes 4 has_many :liked_users, through: :likes, source: :user 5 has_many :member_comments 6 has_many :member, through: :member_comments 7 8 accepts_nested_attributes_for :member_comments 9end
model
1class Like < ApplicationRecord 2 belongs_to :user_id 3 belongs_to :comment_id 4 validates_uniqueness_of :comment_id, scope: :user_id 5end
controller
1class LikesController < ApplicationController 2 def create 3 @like = current_user.likes.create(comment_id: params[:comment_id]) 4 redirect_to comments_path 5 end 6 7 def destroy 8 @like = Like.find_by(comment_id: params[:comment_id], user_id: current_user.id) 9 @like.destroy 10 redirect_back(fallback_location: root_path) 11 end 12 13end
controller
1class CommentsController < ApplicationController 2 3 before_action :authenticate_user!, only: [:create, :show] 4 5 def index 6 @comments = Comment.all 7 @comment = Comment.new 8 end 9 10 def new 11 @comment = Comment.new 12 @comment.member_comments.build 13 end 14 15 def create 16 # binding.pry 17 @comment = Comment.new(comment_params) 18 @comment.user_id = current_user.id 19 20 if @comment.save 21 redirect_to comments_path 22 else 23 render :new 24 end 25 end 26 27 def show 28 @comment = Comment.find(params[:id]) 29 @like = Like.new 30 31 end 32 33 def update 34 end 35 36 def destory 37 comment = Comment.find(params[:id]) 38 comment.destory 39 end 40 41 private 42 def comment_params 43 params.require(:comment).permit(:place, :text, :image, { :member_ids => [] }) 44 end 45 46end
haml
1.contents 2 .content 3 .content__info 4 .content__left-name 5 - @comment.member.each do |m| 6 = m.name 7 .content__right-place 8 = @comment.place 9 .image 10 = image_tag@comment.image 11 .comments 12 .comment 13 = @comment.text 14 %h3 15 いいね件数: #{@comment.likes.count} 16 - if current_user.already_liked?(@comment) 17 = button_to 'いいねを取り消す', post_like_path(@comment), method: :delete 18 - else 19 = button_to 'いいね', post_likes_path(@comment) 20 %h2 いいねしたユーザー 21 - @comment.liked_users.each do |user| 22 %li= user.name
haml
1.contents 2 - @comments.each do |comment| 3 .content 4 .content__left 5 .content__left--place 6 = comment.place 7 .content__right 8 .content__right-name 9 - comment.member.each do |member| 10 = member.name 11 =link_to '詳細', "comments/#{comment.id}", method: :get
試したこと
{@comment.likes.count}の部分の名前が違っているのかと思い
commentsやlikeにしてみた
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー