前提・実現したいこと
投稿の詳細のページに飛んだ時にその投稿にいいね機能を付けたい
いろいろなサイトをみながら作って今回は
https://qiita.com/jaramon/items/248bcb4b56e9fed8fc90
この記事を参考に作ってみた。
発生している問題・エラーメッセージ
NoMethodError in Comments#show /app/views/comments/show.html.haml where line #14 raised: undefined method `keys' for #<Comment:0x00007f855e5328a8>
該当のソースコード
migrate
1class CreateLikes < ActiveRecord::Migration[5.2] 2 def change 3 create_table :likes do |t| 4 t.references :member_id, foreign_key: true 5 t.references :comment_id, foreign_key: true 6 t.timestamps 7 end 8 end 9end
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 :likes_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, dependent: :destroy 4 has_many :iine_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 9 def iine(user) 10 likes.create(user_id: user.id) 11 end 12 13 def uniine 14 likes.find_by(user_id: user.id).destroy 15 end 16 17 def iine?(user) 18 iine_users.include?(user) 19 end 20 21end
model
1class Like < ApplicationRecord 2 belongs_to :user 3 belongs_to :comment 4 validates :user_id, presence: true 5 validates :comment_id, presence: true 6end
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] 8 resources :likes, only: [:create, :destroy] 9end
class UserController < ApplicationController def index @users = User.all end def show @user = User.find(params[:id]) end end
controller
1class LikesController < ApplicationController 2 def create 3 @comment = Comment.find(params[:comment_id]) 4 @comment.iine(current_user) 5 # redirect_to comments_path 6 end 7 8 def destroy 9 @comment = Like.find_by(params[:id]).comment 10 @comment.uniine(current_user) 11 end 12 13end
controller
1class CommentsController < ApplicationController 2 before_action :authenticate_user!, only: [:create, :show] 3 4 def index 5 @comments = Comment.all 6 @comment = Comment.new 7 end 8 9 def new 10 @comment = Comment.new 11 @comment.member_comments.build 12 end 13 14 def create 15 # binding.pry 16 @comment = Comment.new(comment_params) 17 @comment.user_id = current_user.id 18 19 if @comment.save 20 redirect_to comments_path 21 else 22 render :new 23 end 24 end 25 26 def show 27 @comment = Comment.find(params[:id]) 28 @like = Like.new 29 end 30 31 def update 32 end 33 34 def destory 35 comment = Comment.find(params[:id]) 36 comment.destory 37 end 38 39 private 40 def comment_params 41 params.require(:comment).permit(:place, :text, :image, { :member_ids => [] }) 42 end 43 44end
index.html.haml
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
show.html.haml
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 = render "likes/like", @comment
_link.html.erb
erb
1<% if !current_user?(comment.user) %> 2 <span class="like"> 3 <% if comment.iine?(current_user) %> 4 <%= form_for(comment.likes.find_by(user_id: current_user.id), method: :delete, remote: true) do |f| %> 5 <%= button_tag(class: "btn btn-default btn-xs") do %> 6 <%= content_tag :span, "#", class: "glyphicon glyphicon-heart" %> 7 <% end %> 8 <% end %> 9 <% else %> 10 <%= form_for(comment.likes.build, remote: true) do |f| %> 11 <div><%= hidden_field_tag :comment_id, comment.id %></div> 12 <%= button_tag(class: "btn btn-default btn-xs") do %> 13 <%= content_tag :span, "#", class: "glyphicon glyphicon-heart-empty" %> 14 <% end %> 15 <% end %> 16 <% end %> 17 </span> 18<% end %>
試したこと
エラー文からキーが定義されていないのかと思い
@comments.idにすると
undefined method `keys' for 1:Integer
の内容にエラーが変わる。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー