Home>

I want to implement a nice feature for posting
https://qiita.com/nojinoji/items/2c66499848d882c31ffa
I wrote the code referring to this site
I get an error when I go to the details page

Error message
NoMethodError in Comments # show
Showing /app/views/comments/show.html.haml where line # 15 raised:
undefined method `likes' for nil: NilClass
Applicable source code
Rails.application.routes.draw do
  devise_for: users
  resources: 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] do
    resources: likes, only: [: create,: destroy]
  end
end
class User</pre>
<pre><code data-language = "model">class Comment</pre>
<pre><code data-language = "model">class Like</pre>
<pre><code data-language = "controller">class LikesController</pre>
<pre><code data-language = "controller">class CommentsController  []})
    end
end
. 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
    % h3
      Likes: #{@comment.likes.count}
    -if current_user.already_liked? (@ comment)
      = button_to 'Cancel Like', post_like_path (@comment), method:: delete
    -else
      = button_to 'Like', post_likes_path (@comment)
    % h2 Liked User
    [email protected] comment.liked_users.each do | user |
      % li = user.name
. 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

I wonder if the name of {@ comment.likes.count} is different
I tried comments and like

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

  • Answer # 1

    You are told that @comment is nil.
    Then
    @comment = Comment.find (params [: id])
    Is nil, ie a non-existent id is passed

    I thought, but then find wouldn't throw an exception and return nil,

    Are parameters displayed at the bottom of the screen where the error occurred?
    If not, see log.
    Check the id there and check if there is a comment with that id

    Additional
    If @comment = Comment.find (params [: id]) is nil, @ comment.XXX at the top of the view should not get the same error.
    That means that there is no difference-nor =
    I haven't done haml yet, butlikes: # {@ comment.likes.count}Just make sure you try= "likes: # {@ comment.likes.count}"

  • Answer # 2

    Migration file has a mistake It was caused by accidentally creating it in the database.

    class CreateLikes
    class CreateLikes