Home>

I want to add a like function to a post when I jump to the post details page
While looking at various sites, this time
https://qiita.com/jaramon/items/248bcb4b56e9fed8fc90
I made this article for reference.

Error message
NoMethodError in Comments # show
/app/views/comments/show.html.haml where line # 14 raised:
undefined method `keys' for #<Comment: 0x00007f855e5328a8>
Applicable source code
class CreateLikes</pre>
<pre><code data-language = "model">class User</pre>
<pre><code data-language = "model">class Comment</pre>
<pre><code data-language = "model">class Like</pre>
<pre><code data-language = "routes">Rails.application.routes.draw do
  devise_for: usersresources: users, only: [: index,: show]
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root to: 'posts # index'
  resources: posts, only: [: index]
  resources: comments, only: [: index,: new,: create,: show,: destory,: edit]
  resources: likes, only: [: create,: destroy]
end
class UserController</pre>
<pre><code data-language = "controller">class LikesController</pre>
<pre><code data-language = "controller">class CommentsController  []})
    end
end
index.html.haml
. contents
  [email protected] comments.each do | comment |
    .content
      .content__left
        .content__left--place
          = comment.place
      .content__right
        .content__right-name
          -comment.member.each do | member |
            = member.name
      = link_to 'Learn more', "comments/# {comment.id}", method:: get
show.html.haml
. contents
  .content
    .content__info
      .content__left-name
        [email protected] comment.member.each do | m |
          = m.name
      .content__right-place
        = @ comment.place
    .image
      = [email protected]
    .comments
      .comment
        = @ comment.text
    = render "likes/like", @comment
_link.html.erb
<% if! current_user? (comment.user)%>
    <span>
    <% if comment.iine? (current_user)%>
      <% = form_for (comment.likes.find_by (user_id: current_user.id), method:: delete, remote: true) do | f |%>
        <% = button_tag (class: "btn btn-default btn-xs") do%>
          <% = content_tag: span, "#", class: "glyphicon glyphicon-heart"%>
        <% end%>
      <% end%>
    <% else%>
      <% = form_for (comment.likes.build, remote: true) do | f |%>
        <% = hidden_field_tag: comment_id, comment.id%>
        <% = button_tag (class: "btn btn-default btn-xs") do%>
          <% = content_tag: span, "#", class: "glyphicon glyphicon-heart-empty"%>
        <% end%>
      <% end%>
    <% end%>
  </span>
<% end%>

I wonder if the key is not defined from the error statement
@ comments.id
undefined method `keys' for 1: Integer
The error changes to the contents of.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.