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
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
Please provide more detailed information here.
-
Answer # 1
-
Answer # 2
Migration file has a mistake It was caused by accidentally creating it in the database.
class CreateLikes
class CreateLikes
Related articles
- ruby on rails - i can't implement the like function
- ruby - [rails] i want to implement a function to exclude (unlink) group participants
- ruby on rails - rails s can't
- ruby on rails - i want to implement a function for administrator users to register luggage for general users in rails
- ruby - i want to implement google authentication/sns authentication without using devise
- ruby - i can't get the data with json
- ruby on rails - how to implement a sort function by the value of the statistical column that aggregates the values of related
- ruby - how to implement the search function
- ruby on rails - i want to implement a report function, but i get an actioncontroller :: urlgenerationerror
- ruby on rails - i want to implement a function (destroy action) that can delete posted messages, but i am getting an active reco
- ruby - i want to implement a level-up system
- ruby - i want to implement the "like" function, but i get "nomethoderror in messages # index" in the view fi
- ruby - i can't bundle install
- ruby on rails - rails60 i can't log in
- ruby - i can't edit all at once with unknown attribute'' for it
- ruby - unable to implement passwordless user edit function using devise
- ruby - in the api directory created to implement the incremental search, the id cannot be acquired properly! !!
- ruby - can't rails new on vagrant
- ruby on rails 5 - i want to implement the rails search function, but i get an error after searching
- ruby - i can't get git pull request
Related questions
- html - i want to set the conditions to be displayed in collection_check_boxes
- ruby - i get an error in the view while implementing carriewave
- ruby - i get an error saying undefined method `resize_to_limit'for
- [ruby] i'm having trouble with nomethoderror
- i'm having trouble with ruby's word bingo algorithm
- ruby - unable to open rails server
- ruby - if i include the'omniauth-facebook'gem, do i need to install the'omniauth' gem as well?
- ruby - even if each statement is used, the contents of the db cannot be displayed in order and an error occurs
- ruby - [search function] acquisition of products associated with users
- ruby - passing variables using render partial
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, but
likes: # {@ comment.likes.count}
Just make sure you try= "likes: # {@ comment.likes.count}"