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

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

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

Haml(HTML abstraction markup language)は、HTML/XHTMLを効率的に記述するためのマークアップ言語および記法です。

Ruby

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

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

1237閲覧

いいね機能の実装ができない

nasuk47

総合スコア311

Haml

Haml(HTML abstraction markup language)は、HTML/XHTMLを効率的に記述するためのマークアップ言語および記法です。

Ruby

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

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2019/11/25 16:39

編集2019/11/25 23:47

前提・実現したいこと

投稿の詳細のページに飛んだ時にその投稿にいいね機能を付けたい
いろいろなサイトをみながら作って今回は
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/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

winterboum

2019/11/25 21:29

viewのcodeがhamlとかerbとかのラベルになってますが、ここは show.html.hamlとかファイル名で書いていただけないですか。 どれがshowなのか探すのがしんどい
nasuk47

2019/11/25 23:48

すみません。 .をつけると表示されなくなるので見出しを付けました。
guest

回答1

0

自己解決

変更前

migration

1class CreateLikes < ActiveRecord::Migration[5.2] 2 def change 3 create_table :likes do |t| 4 t.references :comment, foreign_key: true 5 t.references :member, foreign_key: true 6 7 t.timestamps 8 end 9 end 10end

変更後

migration

1class CreateLikes < ActiveRecord::Migration[5.2] 2 def change 3 create_table :likes do |t| 4 t.references :comment, foreign_key: true 5 t.references :user, foreign_key: true 6 7 t.timestamps 8 end 9 end 10end
member_idcomment_id
user_idcomment_id

likesテーブルのカラム名が間違っているせいでエラーが起きていたみたいです。

投稿2019/11/26 03:41

nasuk47

総合スコア311

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

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

winterboum

2019/11/26 07:30

原因判ってよかったですね
nasuk47

2019/11/26 07:36

はい。非常に助かりました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問