Reputation: 1
error message: undefined method `id' for nil:NilClass In the postscontroller:
def like
if current_user.voted_for? @post
@post.unliked_by current_user
else
@post.liked_by current_user
end
end
In the _like_link.html.erb:
<% vote_text = current_user&.voted_for?(post) ? "unlike" : "Like" %>
<%= button_to vote_text, like_post_path(post), method: :put, remote: :true, class:'like-post' %>
In the _likes.html.erb:
<%= pluralize(post.get_likes.size, 'Like') %>
In the _post.html.erb
<div>
<%= render 'posts/likes', post:@post %>
<%= render 'posts/like_link', post:@post %>
</div>
In the routes
resources :posts do
resources :comments
member do
put 'like', to: 'posts#like'
end
end
I'm trying to put a like button in a blog application with in Rails 7. The user can like a post by clicking this button, but this error poped up after i clicked this button. I'm not sure if I am using this gem corrently, please help me to figure out what did i do wrong. And is there any better way to use this gem? thanks!
Upvotes: 0
Views: 27